[英]How to remove object by property in javascript
I would like to know how to remove the object by property in nested array object 我想知道如何通过嵌套数组对象中的属性删除对象
I have whole list of object in sampleobj
, compare each id with apitrans, apifund
, if success is false, remove obj in sampleobj 我在
sampleobj
中有对象的完整列表,将每个id与apitrans, apifund
,如果成功为false,则删除sampleobj中的obj
Remove the object if the success is false, in sampleobj. 如果成功为false,则在sampleobj中删除该对象。
I have tried: 我努力了:
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
}
]```
You can use filter()
to remove elements from array. 您可以使用
filter()
从数组中删除元素。
func
) which takes two objects as parameter and compare id
property of both and check success
property of one of them. func
),比较两个对象的id
属性并检查其中一个对象的success
属性。 filter()
of the given array and put both given objects array [apitrans,apifund]
. filter()
并将两个给定对象都放入数组[apitrans,apifund]
。 some()
method on [apitrans,apifund]
and check if any of them have id
equal the current element using Helper Function. [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)
You can filter out your array with conditions ie filter gives you new array instead of changing the original array 您可以使用条件过滤掉数组,即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);
Or if you want an original array to be modified instead of getting a new array, you can use your given approach with some update ie 或者,如果您希望修改原始数组而不是获取新数组,则可以使用给定的方法进行一些更新,即
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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.