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}]
2 Answers
Reset to default 11Not 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 }