简体   繁体   English

如何根据键:值对从数组中删除对象?

[英]How to remove an object from array based on key: value pair?

Lets say I have this array of objects:假设我有这个对象数组:

[{name: "John", age: "30"},
 {name: "Jane", age: "20"}]

Can I remove an object from that array based on a key value pair?我可以根据键值对从该数组中删除一个对象吗? For example remove object with name: "John"?例如删除名称为:“John”的对象?

You can splice the object containing the name "John"您可以拼接包含名称“John”的对象

 var a=[{name: "John", age: "30"}, {name: "Jane", age: "20"}]; a.forEach((e)=>{ if(e.name=="John") a.splice(a.indexOf(e),1) }) console.log(a)

You can also use reject :您还可以使用reject

var people = [{name: "John", age: "30"},
 {name: "Jane", age: "20"}]
people = people.reject(person => person.name === "John")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 object 数组中取出特定的 object,通过键值对过滤 - Remove a specific object from an object array, filtering by a key and value pair 根据键而不是值从数组中删除对象? - remove an object from array based on key not value? 如何根据键和值比较 object 并从数组中删除 - How to compare object based on key and value and remove from array 如何从具有值作为另一个对象数组的 object 中删除键值对? - How to remove key value pair from an object having values as another array of objects? 如何从所有对象中删除一个简单的特定键值对并添加到数组内的一个特定 object - How to remove a simple specific key value pair from all objects and add to one specific object inside an array 根据对象键值从对象数组中删除对象 - Remove object from array of objects based on object key value Javascript:根据键从 Object 数组中的数组中删除值 - Javascript: Remove Value from array inside an array of Object based on key 如何基于另一个键值将新的键值对添加到数组中的对象 - How to add new key value pair to an object in an array based on another key-value 如何将对象的键值对插入数组? - How to insert key,value pair of object to an array? 如何将数组转换为键值对的对象 - How to Convert an Array into Object of key value pair
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM