简体   繁体   中英

Lodash Remove all but one property from list of objects

Is there a lodash function that can produce the following:

Orignally:

var persons = [{"1":1, "2":2, "3":3}, {"1":12, "2":22, "3":32}];
var result = _.func(persons, "1")

After:

result = [{"1":1},{"1":12}]

Not as a single function.

You can combine _.map() to iterate through the array with _.pick() to reduce each object within it.

var result = _.map(persons, function (p) {
    return _.pick(p, '1');
});

You can also use _.partialRight() to create the iterator function:

var result = _.map(persons, _.partialRight(_.pick, '1'));

I think you're looking for lodash - pick .

var object = { 'a': 1, 'b': '2', 'c': 3 };
_.pick(object, ['a', 'c']); // → { 'a': 1, 'c': 3 }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM