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

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); });   
}

Leave a Comment

tech