_.each javascript code example
Example 1: for each javascript
let numbers = ['one', 'two', 'three', 'four'];
numbers.forEach((num) => {
console.log(num);
});
Example 2: js _.each
_.each([1, 2, 3], alert);
=> alerts each number in turn...
_.each({one: 1, two: 2, three: 3}, alert);
=> alerts each number value in turn...
Example 3: underscore js join
_.reduce(['x', 'y', 'z'], function(accumulator, currentItem) {
return accumulator + currentItem;
});
Example 4: each javascript
function each(collection, action) {
if (Array.isArray(collection)) {
for (var i = 0; i < collection.length; i++) {
action(collection[i], i, collection);
}
} else {
for (var key in collection) {
action(collection[key], key, collection);
}
}
}