Below is the referenceArray to compare and manipulate originalArray
const referenceArray = [
{ name: 'animal', source: ['duck', 'cat'], target: ['water', 'ground'] },
{ name: 'car', source: ['tata', 'kia'], target: ['tiago', 'sector'] },
{ name: 'bike', source: ['honda', 'hero'], target: ['livo', 'xtream'] },
{ name: 'vehicle', source: ['honda', 'hero'], target: ['hard', 'soft'] },
];
And this is I want to change the array originalArray source and target values with the help of referenceArray source and target
if it's matched take name value and set that value to originalArray source and target.
const originalArray = [
{ source: 'water', target: 'hero' },
{ source: 'tata', target: 'ground' },
{ source: 'livo', target: 'kia' },
{ source: 'hero', target: 'sector' },
];
Expected output that is required:
const output1 = [
{ source: 'animal', target: ['bike', 'vehicle'] },
{ source: 'car', target: 'animal' },
{ source: 'bike', target: 'car' },
{ source: ['vehicle', 'bike'], target: 'car' },
];
const output2 = [
{ source: 'animal', target: 'bike' },
{ source: 'animal', target: 'vehicle' },
{ source: 'car', target: 'animal' },
{ source: 'bike', target: 'car' },
{ source: 'vehicle', target: 'car' },
{ source: 'bike', target: 'car' },
];
Either output is acceptable, however output2 is preferred.
I'm confused how to achieve this format without getting key value conflicts.