[英]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.