hi I want to push array of an object to array of an object so the the new property will push to the array of object based on the same _id
this is the original data :
const data =[
{
foo:foo1,
data:{
_id:"a1",
man:2
}
},
{
foo:foo1,
data:{
_id:"a1",
man:2
}
}
]
this is the data that I want to put on my original data
const d = [{
_id:"a1",
women:4,
}]
And the desired output is:
const data =[
{
foo:foo1,
data:{
_id:"a1",
man:2,
women:4
}
},
{
foo:foo1,
data:{
_id:"a1",
man:2,
women:4
}
}
]
I think it can be done using for loop and check if the _id is the same and push it to the object, but is there any better way? or using lodash? any idea? thanks in advance
>Solution :
You can try this!
const d = [{
_id:"a1",
women:4,
}
]
const data =[
{
foo:"foo1",
data:{
_id:"a1",
man:2
}
},
{
foo:"foo1",
data:{
_id:"a1",
man:2
}
}
]
var arr = data.map(function(obj, i){
d.map(function(o,i){
if(obj.data._id == o._id)
{
obj.data.women = o.women;
}
});
return obj;
});
console.log(arr);