I have a scenario where I have two objects. The first object contains name and id, while the second object has some fields along with the id field from the first object.
For Example:
FirstObj = [{
_id: '48765465f42424',
Name : 'Sample'
},{
_id: '48765465f654654',
Name : 'Sample1'
}]
secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : { IsActive : true,
FirstObjid : '48765465f42424'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f654654'
}
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : { IsActive : true,
FirstObjid : '48765465f654654'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f42424'
},
}]
}]
In this case, I need to compare both objects and add the corresponding name from the first object to the secondobj's subobj
alongside the FirstObjid
field.
The Expected Output should be:
secondObj = [{
Field1 : 5464,
subarray : [{
Field2 : 14654,
Field3 : { IsActive : true,
FirstObjid : '48765465f42424',
Name : 'Sample'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f654654',
Name : 'Sample1'
}
}]
},
{
Field1 : 2145,
subarray : [{
Field2 : 544644,
Field3 : { IsActive : true,
FirstObjid : '48765465f654654',
Name : 'Sample1'
},
Field4 : { IsActive : true,
FirstObjid : '48765465f42424',
Name : 'Sample'
},
}]
}]
If you are thinking how to achieve this, keep reading!