如何在javascript

时间:2019-05-03 04:46:07

标签: javascript jquery arrays object

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

我在sampleobj中拥有对象的完整列表,将每个id与apitrans, apifund进行比较,如果成功则为false,则删除sampleobj中的obj

如果成功,则删除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
  }
]```

2 个答案:

答案 0 :(得分:1)

您可以使用filter()从数组中删除元素。

  • 创建一个辅助函数(func),该函数将两个对象作为参数并比较两个对象的id属性并检查其中一个的success属性。
  • 然后使用给定数组的filter()并将两个给定对象放入数组[apitrans,apifund]
  • 然后在some()上使用[apitrans,apifund]方法,并使用Helper Function检查其中是否有id等于当前元素。

var arr=[ { 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" }

const func = (obj1,obj2) => obj1.id === obj2.id && obj2.success

const res = arr.filter(x => [apitrans,apifund].some(a => func(x,a)));
console.log(res)

答案 1 :(得分:0)

您可以使用条件过滤掉数组,即filter为您提供新的数组,而不是更改原始数组

var arr = [{
    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"
}



var filter = arr.filter(function(item) {
  //console.log(item);
  if (item.id === apitrans.id && apitrans.success) {
    return item
  }

});
console.log(filter);

或者,如果您希望修改原始数组而不是获取新数组,则可以使用给定的方法进行一些更新,即

var arr = [{
    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"
}


arr.forEach(e => {
  if (e.id === "trans" && apitrans.success == true) {
    Object.assign(e, apitrans);
  } else if (e.id === "fund" && apifund.success == true) {
    Object.assign(e, apifund);
  } else {
    // if success false remove the object.
    var index = arr.indexOf(e);
    arr.splice(index, 1);
  }
})
console.log("resulted original arr", arr)