sowdri
sowdri

Reputation: 2243

Lodash: How to add new field to all values in collection

Example:

var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}]  
var newArr = _.enhance(arr, { married : false });

console.log(newArr); // [{name: 'a', age: 23, married : false}, {name: 'b', age: 24, married : false}]

I'm looking for something to do this. Note, enhance is not present in lodash. Is it possible to do this with lodash?
If not -- possible addition?

Thanks,

Upvotes: 40

Views: 59821

Answers (6)

mfink
mfink

Reputation: 1380

ES6 with Map and Spread

const arr = [{name: 'a', age: 23}, {name: 'b', age: 24}, {name: 'c', age: 25}];
const newArr = arr.map(el => ({ ...el, married: false }));

console.log(newArr);  
// [{age: 23, married: false, name: 'a'}, {age: 24, married: false, name: 'b'}, {name: 'c', age: 25, married: false}]


Note: Arrow functions returing an object literal need to wrap the object with parenthesis, e.g., () => ({})

Upvotes: 7

Janaka Pushpakumara
Janaka Pushpakumara

Reputation: 5107

I use ES6 syntax. I think this will help to u.

var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];    
arr.map((element) => {
   return element.married = false;
});
console.log(arr); // [{name: 'a', age: 23, married : false}, {name: 'b', age: 24, married : false}] 

Upvotes: 16

Maurits Rijk
Maurits Rijk

Reputation: 9985

Using lodash and ES6 arrow notation the solution can become quite short:

const newArr = _.map(arr, o => _.extend({married: false}, o));

Note: I use the object {married: false} as the first argument since mutating this object leaves the original array intact. This means however that married becomes the first field in the resulting object, but this is probably not relevant.

Upvotes: 18

Vee6
Vee6

Reputation: 1577

This comes late and it doesn't involve lodash, but I think the cleanest solution if you want the old data mutable is to use a classic for loop iteration. This way you won't load up the memory with a new array.

Upvotes: -3

dbriggs
dbriggs

Reputation: 35

The function lodash has that is closest to what you want is merge: http://lodash.com/docs#merge

The only slight difference between how merge works and what you want to do is you would need to have an array of equal length to arr all that looks like:

var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];
var marriedArr = [{married : false}, {married : false}];
var newArr = _.merge(arr, marriedArr);
console.log(newArr);

If you attempt to do:

var newArr = _.merge(arr, {married : false});

Then merge will work just like concat.

Upvotes: 1

Evan Davis
Evan Davis

Reputation: 36592

You probably want to extend each of your objects.

mu is too short sort of killed my wordplay while making an excellent point. Updated to create an entirely new array.

var arr = [{name: 'a', age: 23}, {name: 'b', age: 24}];

var newArr = _.map(arr, function(element) { 
     return _.extend({}, element, {married: false});
});

If you want to add it to the library,

_.enhance = function(list, source) {
    return _.map(list, function(element) { return _.extend({}, element, source); });   
}

Upvotes: 58

Related Questions