如何使用Lodash删除对象(查找和删除)

时间:2017-12-01 10:50:57

标签: javascript lodash

我希望使用 Lodash 删除,该对象使用uuid:xxx而非xxx2。

如何使用 Lodash 制作此内容?

[
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
]

我的实际代码:

_.forEach(this.objs, (obj, index, collection) => {
   _.remove(obj, {uuid})
})

2 个答案:

答案 0 :(得分:2)

您可以简单地使用_.remove方法。



var objects = [
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
];

_.remove(objects, o => o.uuid === 'xxx2');

console.log(objects);

<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

您可以Array.prototype.filter()使用

&#13;
&#13;
let arr = [
    {
        "uuid": "xxx",
        "name": "test"
    },
    {
        "uuid": "xxx2",
        "name": "test 2"
    }
]


arr = arr.filter(e => e.uuid != 'xxx2');

console.log(arr);
&#13;
&#13;
&#13;