基于对象属性删除数组元素

编程入门 行业动态 更新时间:2024-10-27 02:29:56
本文介绍了基于对象属性删除数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有像这样对象的数组:

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.

更多推荐

基于对象属性删除数组元素

本文发布于:2023-10-18 02:18:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502791.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   属性   元素   对象

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!