I have 2 arrays.
const history = [
{ type: 'change', old: 1, new: 2 },
{ type: 'change', old: 3, new: 4 },
];
const contents = [
{ id: 1, info: 'infor1' },
{ id: 2, info: 'infor2' },
{ id: 3, info: 'infor3' },
{ id: 4, info: 'infor4' },
];
And the array in result
const detailHistory = [
{ type: 'change', old: { id: 1, info: 'infor1' }, new: { id: 2, info: 'infor2' } },
{ type: 'change', old: { id: 3, info: 'infor3' }, new: { id: 4, info: 'infor4' } },
];
How can I create detailHistory without nested looping from history and contents
First create a Map for the
contents, so you can retrieve one of its objects by id. Once you have that, it comes down to mapping thehistoryobjects:Be aware that
oldandnewwill get object references to the original objects incontents... they are not copied. If this is undesired, then use a (shallow) copy at the moment you map them into the result: