How to merge the property with same key in two object array?
You could take a Map
with all addresses and then map new object with extended properties of the map.
This approach takes all properties of address
objects.
var people = [{ id: "001", name: "David", age: 29 }, { id: "002", name: "Lucia", age: 41 }, { id: "003", name: "Steve", age: 18 }], address = [{ id: "001", city: "Barcelona" }, { id: "002", city: "Paris" }, {}, { id: "003", city: "Tokyo" }, { id: "004", city: "Barcelona" }], map = new Map(address.map(o => [o.id, o])), result = people.map(o => Object.assign({}, o, map.get(o.id)));
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Combining the content of multiple objects that have the same key with that key as the property in JavaScript
You can iterate over the object keys and create a new object out of their aggregation.
You can use ES6 spread operator (...) which allows us to quickly copy all or part of an existing array or object into another array or object.
const cat1 = { med1: { a: 10, b: 12 }, med2: { c: 14, d: 16 } };
const cat2 = { med1: { e: 18, f: 20 }, med2: { g: 22, h: 24 } };
let resultObject = {};
Object.keys(cat1).map(key => { // iterate over the keys
resultObject = {
...resultObject,
[key]: {...cat1[key], ...cat2[key]} // merge two objects
}
return;
});
console.log(resultObject);
Merge objects with the same key in 2 arrays into one array in Javascript/React
You could take a dynamic approach by using an object with wanted keys fro the final result.
const
itemsFr = [{ title: "Bonjour" }, { title: "Bonne Nuit" }],
itemsEn = [{ title: "Good Morning" }, { title: "Good Night" }],
result = Object
.entries({ fr: itemsFr, en: itemsEn })
.reduce((r, [key, a]) => a.map(({ title }, i) => ({
title: { ...r[i]?.title, [key]: title }
})), []);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Merging Array of Objects with same key
To merge the 2 objects inside the array into 1, this involves
Step 1. extracting the required elements from the 2nd object
Step 2. adding it into the object of the first.
Suppose the array is read into a variable data
, then this is 1 way we can extract it in Step 1.
let {
aggregates: {
VALVAL_ALT_CMB_AMT
},
} = data[1];
The value paired to the VALVAL_ALT_CMB_AMT
key of the 2nd object is stored inside a variable VALVAL_ALT_CMB_AMT
. You can read more about Object Destructuring in Javascript.
For Step 2, we will add the value into the first variable. We can also think of objects to have similar syntax to maps, hence this is 1 way we can do it.
let first = data[0];
first["aggregates"]["VALVAL_ALT_CMB_AMT_1"] = VALVAL_ALT_CMB_AMT;
Extracting and adding the elements this way forms a shallow copy of the variable VALVAL_ALT_CMB_AMT
, now as a value of VALVAL_ALT_CMB_AMT_1
. Consequently, it is not advisable to delete the actual object in position 1 from the original array. Instead, it might be better to return the new object first
in a new array.
const result = [first];
return result;
Related Topics
Multi Select Drop Down With Checkbox
Is It Okay to Call Onclick on an Anchor Tag in Reactjs
How to Set Maximum Length in Input Type=Number
How to Format Material Datepicker Date Value to "Mm-Dd-Yyy" Format in Angular 6
Progressive Web App - Service Worker Not Serving Start_Url
How to Capture the Browser Window Close Event
Easiest Way to Mask Characters in Html(5) Text Input
Converting Json Object to CSV Format in JavaScript
Select Drop Down Text-Align Center in Safari
Disable Scrolling When Touch Moving Certain Element
How to Get Pasted Value from Reactjs Onpaste Event
React Open Modal Window on Click in Another Component
How to Get Input Value Dynamically Using Jquery
Can Vue-Router Open a Link in a New Tab
Can We Insert JavaScript into Any Webpage Loaded in the Browser