I would like to know how to update an array of objects list using javascript.
How to update the objList
with the newObjList
by matching the id
and name
.
If the id
and name
matches, then update the objList
nested array object.
var result = updateObj(objList, newObjList);
console.log(result);
function updateObj(list, newObjList){
var flatarr = list.map(e=>Object.entries(e).map(([k, val]) => val)).flat(3);
var filterService = newObjList.filter(e=>e.name=="service");
//got stuck
}
var newObjList=[{
"id": "service",
"name": "bank",
"amount": 2000
},{
"id": "service",
"name": "credit",
"amount": 5000
}]
var objList=[
{
"btob": [{
"id": "service",
"name": "bank",
"amount": 1000
},{
"id": "fund",
"name": "bank",
"amount": 2000
},{
"id": "others",
"name": "bank",
"amount": 5000
}]
},{
"ctob":[{
"id": "service",
"name": "credit",
"amount": 1000,
"rate": 0.4
},{
"id": "fund",
"name": "credit",
"amount": 3000,
"rate": 0.2
},{
"id": "others",
"name": "credit",
"amount": 4000,
"rate": 0.6
}]
}]
}]
Expected Output:
var result=[
{
"btob": [{
"id": "service",
"name": "bank",
"amount": 2000
},{
"id": "fund",
"name": "bank",
"amount": 2000
},{
"id": "others",
"name": "bank",
"amount": 5000
}]
},{
"ctob":[{
"id": "service",
"name": "credit",
"amount": 5000,
"rate": 0.4
},{
"id": "fund",
"name": "credit",
"amount": 3000,
"rate": 0.2
},{
"id": "others",
"name": "credit",
"amount": 4000,
"rate": 0.6
}]
}]
}]