最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Lodash Remove all but one property from list of objects - Stack Overflow

programmeradmin0浏览0评论

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}]

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}]

Share Improve this question asked Mar 27, 2016 at 4:53 ClickThisNickClickThisNick 5,2509 gold badges47 silver badges70 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 11

Not as a single function.

You can bine _.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 }
发布评论

评论列表(0)

  1. 暂无评论