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

我想知道如何通过嵌套数组对象中的属性删除对象


我在中有对象的完整列表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 1

var 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 differs

if error, status error, or success false remove obj in sampleobj


var 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 Output

result: [

  {

    id: "trans",

    amount: "100",

    fee: 2

  }

]```


Helenr
浏览 175回答 2
2回答
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript