强调:从对象的数组中删除所有键/值对
问题描述:
有从对象的数组中删除所有键/值对的智能下划线的方式?
Is there a "smart" underscore way of removing all key/value pairs from an array of object?
例如。我有以下数组:
var arr = [
{ q: "Lorem ipsum dolor sit.", c: false },
{ q: "Provident perferendis veniam similique!", c: false },
{ q: "Assumenda, commodi blanditiis deserunt?", c: true },
{ q: "Iusto, dolores ea iste.", c: false },
];
和我想要得到以下内容:
and I want to get the following:
var newArr = [
{ q: "Lorem ipsum dolor sit." },
{ q: "Provident perferendis veniam similique!" },
{ q: "Assumenda, commodi blanditiis deserunt?" },
{ q: "Iusto, dolores ea iste." },
];
我可以与下面的JS这方面的工作,但我的解决方案不是真正的快乐:
I can get this working with the JS below, but not really happy with my solutions:
for (var i = 0; i < arr.length; i++) {
delete arr[i].c;
};
任何建议多AP preciated。
Any suggestions much appreciated.
答
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'); });