The following does not merge the two JSON object files correctly.
Is it because I'm trying to do a "deep" clone during the merge
or is it because I'm doing something else not permitted?
<script>
function showJson(obj) { return JSON.stringify(obj,null,2); }
const obj1 = {
'records': [
{
'fname': 'Ally',
'lname': 'Oop',
'ssn': 1
},
{
'fname': 'Betty',
'lname': 'Boop',
'ssn': 2
}
]
};
const obj2 = {
'records': [
{
'fname': 'Betty',
'lname': 'Boop',
'ssn': 2
},
{
'fname': 'Elmer',
'lname': 'Fudd',
'ssn': 5
}
]
};
console.log('obj1'); console.log(showJson(obj1));
console.log('obj2'); console.log(showJson(obj2));
let obj3 = { ...obj1, ...obj2 } // merge into new object
console.log('both'); console.log(showJson(obj3));
</script>
The resulting object does not copy the 1st entry of 'obj1'
or is it just copying 'obj2' contents only?
Am I using the '...' (spread) operator incorrectly?
I am expecting a total of 3 records in the new object.