Skip to content Skip to sidebar Skip to footer

Underscore: Remove All Key/value Pairs From An Array Of Object

Is there a 'smart' underscore way of removing all key/value pairs from an array of object? e.g. I have following array: var arr = [ { q: 'Lorem ipsum dolor sit.', c: false

Solution 1:

You can use map and omit in conjunction to exclude specific properties, like this:

var newArr = _.map(arr, function(o) { return _.omit(o, 'c'); });

Or map and pick to only include specific properties, like this:

var newArr = _.map(arr, function(o) { return _.pick(o, 'q'); });

Solution 2:

For Omit

_.map(arr, _.partial(_.omit, _, 'c'));

For Pick

_.map(arr, _.partial(_.pick, _, 'q'));

Post a Comment for "Underscore: Remove All Key/value Pairs From An Array Of Object"