Book Image

Lo-Dash Essentials

By : Adam Boduch
Book Image

Lo-Dash Essentials

By: Adam Boduch

Overview of this book

Table of Contents (15 chapters)
Lo-Dash Essentials
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Iterating over objects


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.

Basic For Each

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...