强调:从对象的数组中删除所有键/值对 [英] Underscore: remove all key/value pairs from an array of object

查看:144
本文介绍了强调:从对象的数组中删除所有键/值对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有从对象的数组中删除所有键/值对的智能下划线的方式?

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'); });

这篇关于强调:从对象的数组中删除所有键/值对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆