【发布时间】: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