簡體   English   中英

如何根據鍵:值對從數組中刪除對象?

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

假設我有這個對象數組:

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

我可以根據鍵值對從該數組中刪除一個對象嗎? 例如刪除名稱為:“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)

您還可以使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM