I have looked around but didn't exactly find what I'm looking for: I want to append to child object values without merging.
EG:
const ADDRESS = {
address1: {
line1: 'blah 1'
// ...
},
address2: {
line1: 'blah 2'
// ...
},
address3: {
line1: 'blah3'
// ...
},
address4: {
line1: 'blah3'
// ...
}
}
If I use spread operator they get merged, I just want them appended
const addressesappended = {
...ADDRESS.address1,
...ADDRESS.address2
}
DESIRED Result:
{
address1: {
line1: 'blah 1'
// ...
},
address2: {
line1: 'blah 2'
// ...
}
}
CodePudding user response:
You could get the values only.
const
ADDRESS = { address1: { line1: 'blah 1' }, address2: { line1: 'blah 2' }, address3: { line1: 'blah3' }, address4: { line1: 'blah3' } };
result = Object.values(ADDRESS);
console.log(result);
CodePudding user response:
You can clone the whole object using
const result = Object.assign({}, ADDRESS);
you can manually assign the child objects if matches "address"
let result = {};
for (const [key, value] of Object.entries(ADDRESS)) {
if(key.includes("address")) result[key] = value;
}
CodePudding user response:
You will need to declare the keys for the associated spread data in the target object.
const addresses = {
address1: { line1: 'blah 1' },
address2: { line1: 'blah 2' },
address3: { line1: 'blah3' },
address4: { line1: 'blah3' }
};
const addressesAppended = {
address1: { ...addresses.address1 },
address2: { ...addresses.address2 }
};
console.log(addressesAppended);
.as-console-wrapper { top: 0; max-height: 100% !important; }