如何在JavaScript中的嵌套数组对象中删除对象

时间:2019-05-03 03:07:38

标签: javascript jquery arrays object

我想知道如何使用javascript删除嵌套数组中的对象。

我有对象,sampleobj,apifund和apitrans

例如,sampleobj包含整个列表,其中每个对象都通过apitrans,apifund中的ID进行比较。 if the property "success is true" override already existed property, and add additional properties else "success is false",remove the object

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
    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"
    }

Expected Result:(do not display fund since success is false)

    result: [{
        success: true,
        id: "trans",
        tamount: "2000",
        fee: "12"
        amount: "100",
    }]

0 个答案:

没有答案