I have 2 objects, as such:
costsObj = {
Honda: 24000,
BMW: 55000,
Toyota: 22000
};
stockObj = {
Honda: 5,
BMW: 1,
Toyota: 2
}
I want to combine the 2 objects into 1 array of objects that looks like:
carsArr = [
{brand: "Honda", cost: 24000, stock: 5},
{brand: "BMW", cost: 55000, stock: 1},
{brand: "Toyota", cost: 22000, stock: 2}
]
I have achieved this by using map(), for(), and forEach():
// Use costsObj to create new array
carsArr = Object.keys(costsObj).map(brand => {
return { brand, cost: costsObj[brand], stock: 0 };
});
// Add data from stockObj to new carsArr
for (const [key, value] of Object.entries(stockObj)) {
carsArr.forEach((item, index)=>{
if(item.term == key) {
carsArr[index].stock = value;
}
})
};
But this feels clunky and that there could/should be a better way, especially if I want to combine data from more objects in the future, eg:
carsArr = [
{brand: "Honda", model: "Accord", cost: 24000, stock: 5},
{brand: "BMW", model: "3 series", cost: 55000, stock: 1},
{brand: "Toyota", model: "Camry", cost: 22000, stock: 2}
]
Does anyone know if there is a better way to combine the data from many objects into one array of objects?
4
Answers
You can do it in one line like this:
It looks at each of the input objects, and considers the name of the object to be the same as the key that will appear in the final result. Therefore, the object
cost
will have entries using the keycost:
in the result.It uses reduce to iterate over values and place them in an object keyed by the brand. It then uses Object.values() to get just the values of that object, once it has been built.
You can do this like this.
Also you can combine more objects easily.
Use Object.entries to get an array of key value pairs from
costsObj
, then use Array#map to convert each entry to the objects you want.I have found the following code, But it will merge objects and arrays in a specific way.