【问题标题】:How to remove object by property in javascript如何在javascript中按属性删除对象
【发布时间】:2019-05-03 04:46:06
【问题描述】:

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

我在sampleobj 中有完整的对象列表,将每个id 与apitrans, apifund 进行比较,如果成功为假,则删除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
  }
]```

【问题讨论】:

  • 您可以使用filter 例如` const 结果 = sampleobj.filter(o=>o.id===apitrans.id && !apitrans.success)`。相应地改变上述条件。查看更多here
  • 你能解释更多吗..?并告诉我们您到目前为止尝试了什么,因为我没有得到您想要将 sampleobj arr 与两个 obj 进行比较的问题。两个 obj 都具有数组中存在的 id (id-fund n id-trans)
  • @the_ultimate_developer 我已经更新了帖子,添加了我尝试过的内容
  • @the_ultimate_developer 我需要比较apitrans with sampleobj and apifund with sampleobj if success false remove the obj in sampleobj
  • @sowmiya 是的,明白并发布了我的答案

标签: javascript jquery arrays object


【解决方案1】:

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

  • 创建一个辅助函数(func),它接受两个对象作为参数,比较两者的id 属性并检查其中一个对象的success 属性。
  • 然后使用给定数组的filter() 并将两个给定对象数组放入[apitrans,apifund]
  • 然后在[apitrans,apifund] 上使用some() 方法,并使用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)

【讨论】:

  • 谢谢,但如果属性不同,请更新您的答案输入
  • @sowmiya 你没有在问题中提到类似的东西。
  • 是的,抱歉。我还没有,但情况不同,
  • @sowmiya 您应该用完整的信息编辑问题。
  • @sowmiya var apiinsta = { errors: [code:""],id: "insta",tamount: "2000",fee: "12"} 是错误的,因为它包含带有键的数组。 [code:""] 数组不能有键:值。
【解决方案2】:

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

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)

【讨论】:

    猜你喜欢
    • 2022-07-06
    • 2010-09-17
    • 1970-01-01
    • 2020-07-08
    • 1970-01-01
    相关资源
    最近更新 更多