从其他阵列过滤对象

我想实现的是,如果fleetArray中的任何fleet_id存在于om对象中,那么我想从om对象中删除它。 有没有更好的方法来做到这一点?从其他阵列过滤对象

var newObject= {} 

newObject.data=[]

var om = {

"message": "Successful",

"status": 200,

"data": [{

"tag": "",

"array": [

{

"type": 0,

"fleet_id": 23508,

},

{

"type": 10,

"fleet_id": 235089,

}

]

}, {

"tag": "",

"array": [{

"type": 320,

"fleet_id": 23218,

}]

}]

}

var fleetArray = ["23218","23508"];

om.data.forEach(function(api){

//console.log(api)

(api.array).forEach(function(data1){

fleetArray.forEach(function(fleet){

if(fleet==data1.fleet_id){

newObject.data.push(data1)

}

})

})

})

console.log(newObject)

回答:

您可以使用地图和过滤

om.data = om.data.map(e => { 

e.array = e.array.filter(a => ! fleetArray.includes("" + a.fleet_id))

return e;

});

var om = {"message": "Successful","status": 200,"data": [{"tag": "","array": [{"type": 0,"fleet_id": 23508,},{"type": 10,"fleet_id": 235089,}]}, {"tag": "","array": [{"type": 320,"fleet_id": 23218,}]}]}  

var fleetArray = ["23218","23508"];

om.data = om.data.map(e => {

e.array = e.array.filter(a => ! fleetArray.includes("" + a.fleet_id))

return e;

});

console.log(om);

以上是 从其他阵列过滤对象 的全部内容, 来源链接: utcz.com/qa/264959.html

回到顶部