我想知道如何通過嵌套數組對象中的屬性刪除對象我在中有對象的完整列表sampleobj,將每個id與進行比較apitrans, apifund,如果成功為false,則刪除sampleobj中的obj如果成功為false,則在sampleobj中刪除對象。我試過了:var result = sampleobj.foreach(e=>{ if(e.id === "trans" && apitrans.success== true){ Object.assign(e, apitrans); } if(e.id === "fund" && apifund.success== true){ Object.assign(e, apifund); } // if success false remove the object.})//inputs scenario 1var sampleobj=[{ id: "trans", amount: "100", fee: 2 }, { id: "fund", amount: "200", fee: 2 }]var apitrans = { success: true, id: "trans", tamount: "2000", fee: "12" }var apifund = { success: false, id: "fund", tamount: "4000", fee: "10" } //inputs scenario 2 how to do same if property name differsif error, status error, or success false remove obj in sampleobjvar sampleobj=[{ id: "trans", amount: "100", fee: 2 }, { id: "fund", amount: "200", fee: 2 },{ id: "insta", amount: "400", fee: 2 }]var apitrans = {success: true,id: "trans",tamount: "2000",fee: "12"}var apiinsta = { errors: [{code:"error.route.not.supported"}],id: "insta",tamount: "2000",fee: "12"}var apifund = { status: "error", id: "fund", tamount: "4000", fee: "10" }var sampleobj=[{//Expected Outputresult: [ { id: "trans", amount: "100", fee: 2 }]```
如何在JavaScript中按屬性刪除對象
Helenr
2021-04-30 10:13:21