Merge array of objects into a single object [duplicate] - javascript

This question already has answers here:
Flatten array with objects into 1 object
(12 answers)
Closed 1 year ago.
I have an array of objects in this shape;
const objectsArray = [{ projects: [] }, { components: [] }];
I want to merge it into a single object like this;
const mergedObject={ projects: [], components: [] }
Is there a better way of doing it other than this:
Object.keys(objectArray).map(key=>objectArray[key]).reduce((old,item)=>(
{...old,...item}
),{})

This might be a bit simpler:
const objectsArray = [{ projects: [1,2,3] }, { components: [4,5,6] }];
let mergedObjects
objectsArray.forEach(obj => mergedObjects = { ...mergedObjects, ...obj})
console.log(mergedObjects)

Related

Constructing an array of object with two arrays [duplicate]

This question already has answers here:
How to combine two arrays into an array of objects in javascript?
(2 answers)
Combine the values of two arrays into object
(5 answers)
How to create a JS object from arrays [duplicate]
(4 answers)
Closed 6 months ago.
Is there a nice way of transforming a flat javascript array into an array of objects?
An example would be to transform this array.
I'm stuck in trying to merge the two arrays and create an object with the values.
const dataSet = [
{
last_updated: 1662040601,
x: [
1660953600, 1661040000, 1661126400, 1661212800, 1661299200, 1661385600,
1661472000, 1661558400, 1661644800, 1661731200, 1661817600, 1661904000,
],
y: [
0.07, 0.062, 0.06, 0.0725, 0.075, 0.089, 0.0799, 0.1167, 0.089, 0.08,
0.077, 0.0639,
],
},
];
Into this:
const array = [
{ data: 1660953600, value: 0.07 },
{ data: 1661040000, value: 0.062 },
{ data: 1661126400, value: 0.06 },
{ data: 1661212800, value: 0.0725 },
];
My attempt:
const arri = dataSet.map((data) => ({
data: data.x.map((data) => data),
value: data.y,
}));
I hope it will help you..
const array = dataSet[0].x.map((x, i) => ({ data: x, value: dataSet[0].y[i] }));
What do you think about it ?

Create a new array from array of objects [duplicate]

This question already has answers here:
Map array by nested array object property, to an array of strings
(4 answers)
Closed 1 year ago.
I am learning javascript.
I have an array that looks like :
myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
I want to create a new array with ["Value1","Value2"].
How should I do that ?
Thanks a lot !
You can use the Javascript function map() for this. :
const myArray = [{
"item":{
"fields":{
"myfield":"Value1"
}
}
},
{
"item":{
"fields":{
"myfield":"Value2"
}
}
}];
const r = myArray.map(e => e.item.fields.myfield)
console.log(r)

JavaScript: convert an array of string to an array of objects [duplicate]

This question already has answers here:
Convert array of strings into an array of objects
(6 answers)
Closed 2 years ago.
I have an array of strings: const names = ['name1', 'name2', name3']
And I need the following array of objects:
const newArray = [{ name: 'name1' }, { name: 'name2' }, { name: 'name3' }]
How can I create newArray from names, or even possibly convert names itself without creating brand new array?
This can be easily achieved with the map function.
const newArray = names.map((name) => {
return {
name
};
});
map() can do that for you:
const names = ['name1', 'name2', 'name3'];
const newArray = names.map(x=>{return{name:x};});
console.log(newArray);

Convert array to array of objects [duplicate]

This question already has answers here:
Javascript string array to object [duplicate]
(4 answers)
Convert array of string to array of object using es6 or lodash
(4 answers)
Convert array of strings into an array of objects
(6 answers)
JS : Convert Array of Strings to Array of Objects
(1 answer)
Closed 3 years ago.
I have an array like this:
const categories = ["Action", "Comedy", "Thriller", "Drama"]
And I would like to convert it to this format:
const displayedCategories = [{"type": "Action", "isDisplayed": true},{"type": "Comedy", "isDisplayed": true},{"type": "Thriller", "isDisplayed": true},{"type": "Drama", "isDisplayed": true}]
Any advice ? :)
You can do so by using map:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
const newCategories = categories.map(category => ({
type: category,
isDisplayed: true
}));
console.log(newCategories);
You can do so with the map method:
const displayedCategories = categories.map(function(category) {
return { type: category, isDisplayed: true };
});
Maybe like This:
const categories = ["Action", "Comedy", "Thriller", "Drama"];
var out = [];
for(var key in categories){
out.push({"type": categories[key], "isDisplayed": true});
}
console.log(out);
use array.prototype.map to convert the array into an object
categories = catergories.map(val => {
var obj = {
type:val
//more properties go here
}
return obj
}
const categories = ["Action", "Comedy", "Thriller", "Drama"]
const displayedCategories = categories.map(category => { return {"type": category, "isDisplayed": true} })
console.log('displayedCategories :', displayedCategories);

add one object to another object in javascript [duplicate]

This question already has answers here:
How can I merge properties of two JavaScript objects dynamically?
(69 answers)
Closed 4 years ago.
I have 2 objects, and I need to add one object to the other object.
var loc = {
leftArray: [],
topArray: [],
bottomArray: [],
rightArray: []
}
var obj = {
id: "ce",
icon: "logo/image.png",
name: "CE",
type: "type2",
version: 3.4
}
var obj = {
id: "ce",
icon: "logo/image.png",
name: "CE",
type: "type2",
version: 3.4,
leftArray: [],
topArray: [],
bottomArray: [],
rightArray: []
}
Is there any simple way to do this?
You can do it with Object.assign();
obj = Object.assign(obj, loc);
or just
Object.assign(obj, loc);
as T.J. Crowder proposed in his comment.
you can merge them using Spread syntax like
let objOne = { a: 'hi' }
let objTwo = { b: 'hello' }
let objOneAndTwo = { ...objOne, ...objTwo }; // { a: 'hi', b: 'hello' }
or by using Object assign:
let objOneAndTwo = Object.assign({}, a, b)
note that using this way if your objects would have properties with the same name priority will be with the most right object (in this example objectTwo)
additional info:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax

Categories

Resources