Lo-Dash has a few functions that are useful when we need to iterate over the properties of an object in order to fulfill the behavior of our component. We'll start off by exploring some basic iterations. Then, we'll look at how to iterate over inherited object properties, followed by looking at keys and values and simple approaches to iterating over them.
Just as we saw in the previous chapter, objects can be iterated, just as arrays—they're both collections. While the mechanism to do so is slightly different, Lo-Dash abstracts those differences away behind a unified function API, as shown in the following code:
var object = { name: 'Vince', age: 42, occupation: 'Architect' }, result = []; _.forOwn(object, function(value, key) { result.push(key + ': ' + value); }); // → // [ // "name: Vince", // "age: 42", // "occupation: Architect" // ]
The preceding code should look somewhat familiar. It's just like the forEach()
function. Instead...