我希望使用Lodash删除对象,使用uuid: xxx而不是xxx2的对象。
我怎么能用Lodash做这件事?
[
{
"uuid": "xxx",
"name": "test"
},
{
"uuid": "xxx2",
"name": "test 2"
}
]我的实际代码:
_.forEach(this.objs, (obj, index, collection) => {
_.remove(obj, {uuid})
})发布于 2017-12-01 11:00:08
您可以简单地使用.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>
发布于 2017-12-01 10:53:36
你可以使用Array.prototype.filter()
let arr = [
{
"uuid": "xxx",
"name": "test"
},
{
"uuid": "xxx2",
"name": "test 2"
}
]
arr = arr.filter(e => e.uuid != 'xxx2');
console.log(arr);
https://stackoverflow.com/questions/47591797
复制相似问题