How can you merge objects in array of objects?
Clash Royale CLAN TAG#URR8PPP
up vote
7
down vote
favorite
I'm looking for the best solution to merge all objects in one array
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
I want to achieve: name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
What's the best option for that (es6)? Maybe I can do something like that using lodash? Which helpers should I use?
javascript arrays ecmascript-6 lodash
add a comment |Â
up vote
7
down vote
favorite
I'm looking for the best solution to merge all objects in one array
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
I want to achieve: name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
What's the best option for that (es6)? Maybe I can do something like that using lodash? Which helpers should I use?
javascript arrays ecmascript-6 lodash
2
is every object inarrayOfObjects
guaranteed to have the same properties?
â jtate
2 hours ago
add a comment |Â
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I'm looking for the best solution to merge all objects in one array
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
I want to achieve: name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
What's the best option for that (es6)? Maybe I can do something like that using lodash? Which helpers should I use?
javascript arrays ecmascript-6 lodash
I'm looking for the best solution to merge all objects in one array
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
I want to achieve: name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
What's the best option for that (es6)? Maybe I can do something like that using lodash? Which helpers should I use?
javascript arrays ecmascript-6 lodash
javascript arrays ecmascript-6 lodash
edited 9 mins ago
Constantin Chirila
16911
16911
asked 2 hours ago
Rafonix
476
476
2
is every object inarrayOfObjects
guaranteed to have the same properties?
â jtate
2 hours ago
add a comment |Â
2
is every object inarrayOfObjects
guaranteed to have the same properties?
â jtate
2 hours ago
2
2
is every object in
arrayOfObjects
guaranteed to have the same properties?â jtate
2 hours ago
is every object in
arrayOfObjects
guaranteed to have the same properties?â jtate
2 hours ago
add a comment |Â
7 Answers
7
active
oldest
votes
up vote
5
down vote
You could do it like this:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
add a comment |Â
up vote
4
down vote
You could reduce the array by iterating the entries and collecting the values, depending of the keys.
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
add a comment |Â
up vote
2
down vote
easy with lodash:
grouped = _.mapValues(arrayOfObjects[0],
(val, key) => _.map(arrayOfObjects, key))
pure es6
let grouped = ;
for (let obj of arrayOfObjects)
for (let [key, val] of Object.entries(obj))
grouped[key] = (grouped[key] || ).concat(val)
add a comment |Â
up vote
0
down vote
The following should work - uses a few ES6 helpers, but the key is Array#reduce
which is in ES5.
const result = arrayOfObjects.reduce((acc, obj) =>
for (let key in obj)
if (key in acc)
acc[key].push(obj[key]);
else
acc[key] = [obj[key]];
return acc;
, );
add a comment |Â
up vote
0
down vote
Here is a lodash approach
_(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will take care if the objects doesn't have exactly same key sets
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
add a comment |Â
up vote
0
down vote
You can use lodash's mergeWith
like so:
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
Example:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
add a comment |Â
up vote
0
down vote
Short way with array reduce:
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
add a comment |Â
7 Answers
7
active
oldest
votes
7 Answers
7
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You could do it like this:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
add a comment |Â
up vote
5
down vote
You could do it like this:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You could do it like this:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
You could do it like this:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = ;
arrayOfObjects.forEach(item =>
Object.keys(item).forEach(key =>
if (!result[key])
result[key] = ;
result[key].push(item[key]);
);
);
console.log(result);
answered 2 hours ago
ssc-hrep3
4,67521551
4,67521551
add a comment |Â
add a comment |Â
up vote
4
down vote
You could reduce the array by iterating the entries and collecting the values, depending of the keys.
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
add a comment |Â
up vote
4
down vote
You could reduce the array by iterating the entries and collecting the values, depending of the keys.
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
add a comment |Â
up vote
4
down vote
up vote
4
down vote
You could reduce the array by iterating the entries and collecting the values, depending of the keys.
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
You could reduce the array by iterating the entries and collecting the values, depending of the keys.
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
const
array = [ name: 'Fred', surname: 'Shultz' , name: 'Anne', surname: 'Example' ],
result = array.reduce((r, o) => ).push(v));
return r;
, Object.create(null));
console.log(result);
answered 2 hours ago
Nina Scholz
163k1280141
163k1280141
add a comment |Â
add a comment |Â
up vote
2
down vote
easy with lodash:
grouped = _.mapValues(arrayOfObjects[0],
(val, key) => _.map(arrayOfObjects, key))
pure es6
let grouped = ;
for (let obj of arrayOfObjects)
for (let [key, val] of Object.entries(obj))
grouped[key] = (grouped[key] || ).concat(val)
add a comment |Â
up vote
2
down vote
easy with lodash:
grouped = _.mapValues(arrayOfObjects[0],
(val, key) => _.map(arrayOfObjects, key))
pure es6
let grouped = ;
for (let obj of arrayOfObjects)
for (let [key, val] of Object.entries(obj))
grouped[key] = (grouped[key] || ).concat(val)
add a comment |Â
up vote
2
down vote
up vote
2
down vote
easy with lodash:
grouped = _.mapValues(arrayOfObjects[0],
(val, key) => _.map(arrayOfObjects, key))
pure es6
let grouped = ;
for (let obj of arrayOfObjects)
for (let [key, val] of Object.entries(obj))
grouped[key] = (grouped[key] || ).concat(val)
easy with lodash:
grouped = _.mapValues(arrayOfObjects[0],
(val, key) => _.map(arrayOfObjects, key))
pure es6
let grouped = ;
for (let obj of arrayOfObjects)
for (let [key, val] of Object.entries(obj))
grouped[key] = (grouped[key] || ).concat(val)
answered 2 hours ago
georg
140k31191287
140k31191287
add a comment |Â
add a comment |Â
up vote
0
down vote
The following should work - uses a few ES6 helpers, but the key is Array#reduce
which is in ES5.
const result = arrayOfObjects.reduce((acc, obj) =>
for (let key in obj)
if (key in acc)
acc[key].push(obj[key]);
else
acc[key] = [obj[key]];
return acc;
, );
add a comment |Â
up vote
0
down vote
The following should work - uses a few ES6 helpers, but the key is Array#reduce
which is in ES5.
const result = arrayOfObjects.reduce((acc, obj) =>
for (let key in obj)
if (key in acc)
acc[key].push(obj[key]);
else
acc[key] = [obj[key]];
return acc;
, );
add a comment |Â
up vote
0
down vote
up vote
0
down vote
The following should work - uses a few ES6 helpers, but the key is Array#reduce
which is in ES5.
const result = arrayOfObjects.reduce((acc, obj) =>
for (let key in obj)
if (key in acc)
acc[key].push(obj[key]);
else
acc[key] = [obj[key]];
return acc;
, );
The following should work - uses a few ES6 helpers, but the key is Array#reduce
which is in ES5.
const result = arrayOfObjects.reduce((acc, obj) =>
for (let key in obj)
if (key in acc)
acc[key].push(obj[key]);
else
acc[key] = [obj[key]];
return acc;
, );
answered 2 hours ago
Robin Zigmond
88839
88839
add a comment |Â
add a comment |Â
up vote
0
down vote
Here is a lodash approach
_(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will take care if the objects doesn't have exactly same key sets
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
add a comment |Â
up vote
0
down vote
Here is a lodash approach
_(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will take care if the objects doesn't have exactly same key sets
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Here is a lodash approach
_(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will take care if the objects doesn't have exactly same key sets
Here is a lodash approach
_(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
This will take care if the objects doesn't have exactly same key sets
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
var input = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
var res = _(input).flatMap(_.entries).groupBy(0).mapValues(v => _.map(v, 1)).value()
console.log(res);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
edited 1 hour ago
answered 2 hours ago
Koushik Chatterjee
2,5173924
2,5173924
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
add a comment |Â
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
This results in a slightly different output structure than expected.
â ssc-hrep3
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
Ohh, yeah, need to map the value, let me edit
â Koushik Chatterjee
2 hours ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
@ssc-hrep3 see edit, I forget to map it to the expected type of result
â Koushik Chatterjee
1 hour ago
add a comment |Â
up vote
0
down vote
You can use lodash's mergeWith
like so:
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
Example:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
add a comment |Â
up vote
0
down vote
You can use lodash's mergeWith
like so:
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
Example:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
add a comment |Â
up vote
0
down vote
up vote
0
down vote
You can use lodash's mergeWith
like so:
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
Example:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
You can use lodash's mergeWith
like so:
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
Example:
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
const arrayOfObjects = [
name: 'Fred', surname: 'Shultz', name: 'Anne', surname: 'Example'
];
const result = _.mergeWith(, ...arrayOfObjects, (value, objValue) =>
(value || ).concat(objValue)
);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
edited 1 hour ago
answered 1 hour ago
ibrahim mahrir
20.2k41442
20.2k41442
add a comment |Â
add a comment |Â
up vote
0
down vote
Short way with array reduce:
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
add a comment |Â
up vote
0
down vote
Short way with array reduce:
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
add a comment |Â
up vote
0
down vote
up vote
0
down vote
Short way with array reduce:
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
Short way with array reduce:
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
const arrayOfObjects = [
name: "name1", surname: "surname1", name: 'Anne', surname: 'Example', name: 'name3', surname: 'Example3'
];
/*
name: ['Fred', 'Anne'], surname: ['Example', 'Shultz']
*/
var result = arrayOfObjects.reduce((obj,current)=>
(obj['name'] = obj['name'],);
console.log(result);
edited 1 hour ago
answered 1 hour ago
protoproto
47336
47336
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f52761212%2fhow-can-you-merge-objects-in-array-of-objects%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
is every object in
arrayOfObjects
guaranteed to have the same properties?â jtate
2 hours ago