Breaking out of the For Loop: Pragmatic Functional Techniques in JavaScript
While JavaScript’s simplicity and small size is often an advantage, JS programmers sometimes must restort to techniques that are too low-level and convulted for the problem at hand. With no standard library to speak of, convinient abstraction mechanisms available in many other languages are often lacking from JavaScript.
A common scenario for many JS developers is extracting information from a list of objects. Consider the following datastructure.
var sales = [{ customer: 'Kurt', item: 'cup', price: 14.99 },
{ customer: 'Siri', item: 'drone', price: 1095.00 },
{ customer: 'Kurt', item: 'book', price: 22.50 },
{ customer: 'Lisa', item: 't-shirt', price: 25.00 }];
Let’s say we want to know if a particular item has been sold. JavaScript has no any-function, so we have to iterate over the list ourselves, either using recursion or a loop. In this...