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

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

问题描述

我有一个像这样的对象数组:

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?

例如我将如何删除以 'money' 作为字段属性的数组对象?

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

推荐答案

一种可能性:

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

请注意 filter 创建一个新数组.尽管您使用新引用更新了原始变量 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天全站免登陆