Underscore: remove all key/value pairs from an array of object

前端 未结 2 1545
清酒与你
清酒与你 2020-12-28 13:29

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: \"         


        
相关标签:
2条回答
  • 2020-12-28 13:40

    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'); });
    
    0 讨论(0)
  • 2020-12-28 14:04

    For Omit

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

    For Pick

    _.map(arr, _.partial(_.pick, _, 'q'));
    
    0 讨论(0)
提交回复
热议问题