I need to implement a javascript fx to merge associative array that may have a non defined structure.
I mean that sometimes i need to merge
[
{ name: 'Stefano', age:46 },
{ name: 'Name2', age:48 }
]
with
[
{ name: 'Stefano', location:'Italy' },
{ name: 'Name3', Location:'Elsewhere' }
]
and the goal is to obtain an array like this:
[
{ name: 'Stefano', age:46, location:'Italy' },
{ name: 'Name2', age:48 },
{ name: 'Name3', location:'Elsewhere' }
]
as long as to merge array with a different structure
[
{ x:1, y:2, type:'a', category:8 },
{ x:1, y:3, type:'a', category:9 }
]
with
[
{ x:1, y:2, bin:23, site:34 },
{ x:1, y:3, bin:56, site:78 }
]
to obtain an array like this:
[
{ x:1, y:2, type:'a', category:8, bin:23, site:34 },
{ x:1, y:3, type:'a', category:9, bin:56, site:78 }
]
The array are taken from external CSV conversion and the labels/keys are not always the same (they may change accordingly by the user).
Is there a smart way to:
- Detect keys in common with 2 associative array
- Combine their values
- Then merge records with common values?
[{ name: 'Stefano', age:46 }][{ name: 'Name2', age:48 }]but not one array with two elements?[{ name: 'Stefano', age:46 }, { name: 'Name2', age:48 }]