'How to add key value pair to all objects in an array with lodash
I'm looping through an array of objects, each of which has a nested array of objects:
_each(this.props.chartProps.data, function(item){
//item.values is an array of objects
});
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?
Solution 1:[1]
I used a straightforward map array prototype method:
item.values = item.values.map(value => { value.foo = bar; return value; });
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
