How To Compare And Push Field To Sub Array In Object
I have an object which contain name and id. Second object has some fields and that id field from first object. For Example: FirstObj = [{ _id: '48765465f42424', Name : 'Sample
Solution 1:
Try this
secondObj.subarray.forEach(function(item){
if(item.FirstObjid==FirstObj._id)
item.Name = FirstObj.Name
})
Solution 2:
Use map
function and compare the object ids like this
var secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : 'sfsadf',
FirstObjid : '48765465f42424'
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : 'awrfsa',
FirstObjid : '48765465f654654'
}]
}]
varFirstObj = [{
_id: '48765465f42424',
Name : 'Sample'
},{
_id: '48765465f654654',
Name : 'Sample1'
}]
secondObj.subarray = secondObj.map(o => o.subarray.map(k=>FirstObj.map(l=> {
if(l._id == k.FirstObjid){
k.Name = l.Name
}
return o;
})))
console.log(secondObj)
Post a Comment for "How To Compare And Push Field To Sub Array In Object"