I’m looping through an array of objects, each of which has a nested array of objects:
JavaScript
x
4
1
_each(this.props.chartProps.data, function(item){
2
//item.values is an array of objects
3
});
4
I want to add the same key value pair to all of the objects within the nested array. In other words, all of the objects in item.values should have a new key value pair added, call it newpair
.
I’d like to clone it.
Is there a quick lodashian way to do this?
Advertisement
Answer
Something like this ?
JavaScript
1
5
1
function modify(o) { /* set prop here */}
2
3
var objects = _.flatMap(array, function(o) { return o.values; });
4
_.forEach(objects, modify);
5