基于对象属性删除数组元素 [英] Remove array element based on object property

查看:120
本文介绍了基于对象属性删除数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有像这样对象的数组:

I have an array of objects like so:

var myArray = [
    {field: 'id', operator: 'eq', value: id}, 
    {field: 'cStatus', operator: 'eq', value: cStatus}, 
    {field: 'money', operator: 'eq', value: money}
];

我如何删除基于其特性的特定的一个?

How do I remove a specific one based on its property?

例如。我将如何删除与钱的数组对象的字段属性?

e.g. How would I remove the array object with 'money' as the field property?

推荐答案

一种可能性:

myArray = myArray.filter(function( obj ) {
    return obj.field !== 'money';
});

请注意,过滤器创建一个新的数组。虽然你用新的参考更新原始变量 myArray的任何其他变量指的是原始数组不会得到过滤后的数据。请谨慎使用。

Please note that filter creates a new array. Any other variables referring to the original array would not get the filtered data although you update your original variable myArray with the new reference. Use with caution.

这篇关于基于对象属性删除数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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