为了账号安全,请及时绑定邮箱和手机立即绑定

如何在JavaScript中按属性删除对象

如何在JavaScript中按属性删除对象

Helenr 2021-04-30 10:13:21
我想知道如何通过嵌套数组对象中的属性删除对象我在中有对象的完整列表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  }]```
查看完整描述

2 回答

  • 2 回答
  • 0 关注
  • 174 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信