Simple way to turn array of objects into object with grouped keys as arrays
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am looking for a simple way to do the following. I have tried to do this with lodash.reduce
and it is clunky, is there an easier way.
From:
[{a: 'meow'}, {a: 'woof'}]
To:
{a: ['meow', 'woof']}
javascript typescript lodash
add a comment |
I am looking for a simple way to do the following. I have tried to do this with lodash.reduce
and it is clunky, is there an easier way.
From:
[{a: 'meow'}, {a: 'woof'}]
To:
{a: ['meow', 'woof']}
javascript typescript lodash
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33
add a comment |
I am looking for a simple way to do the following. I have tried to do this with lodash.reduce
and it is clunky, is there an easier way.
From:
[{a: 'meow'}, {a: 'woof'}]
To:
{a: ['meow', 'woof']}
javascript typescript lodash
I am looking for a simple way to do the following. I have tried to do this with lodash.reduce
and it is clunky, is there an easier way.
From:
[{a: 'meow'}, {a: 'woof'}]
To:
{a: ['meow', 'woof']}
javascript typescript lodash
javascript typescript lodash
edited Jan 3 at 3:24
ThomasReggi
asked Jan 3 at 3:16
ThomasReggiThomasReggi
18.7k46150277
18.7k46150277
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33
add a comment |
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33
add a comment |
6 Answers
6
active
oldest
votes
You can do that with pure JS, no need of loadash.
Call the reduce
method of arrays on your input array, and reduce the array to an object, looping over the keys of your inner objs:
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
add a comment |
You can use lodash#assignWith to assign all properties their respective values into one object, together with a customizer function to determine how you want to structure the object.
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
Note: To make sure that we don't mutate any of the objects in the data
array, I passed an empty object as the first parameter to act as the destination object.
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
also,_.mergeWith
can be used for the same
– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to uselodash#assignWith
– ryeballar
Jan 3 at 10:20
add a comment |
I had some issues with typescript and lodash.reduce, this worked.
export function getFuncsObject(funcs): Record<Funcs, Case> {
let f = { ...funcs };
f = lodash.mapValues(f, () => );
return f;
}
export function mockMerge(funcs, mocks: Record<Funcs, Case | null>): Record<Funcs, Case> {
const f = getFuncsObject(funcs);
lodash.each(mocks, (v, k) => {
f[k].push(v);
});
return f;
}
add a comment |
One option would be to use two reductions as follows:
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
Not sure if this is clunky compared to your current solution, but it's fairly concise and does not require an external library.
add a comment |
Without using any external libraries or reduce.
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
add a comment |
[{a: 'meow'}, {a: 'woof'}].reduce((m, n) => ({...m, [Object.keys(n)[0]]: (m[Object.keys(n)[0]] && m[Object.keys(n)[0]].concat(Object.values(n)[0])) || .concat(Object.values(n)[0])}), {})
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015878%2fsimple-way-to-turn-array-of-objects-into-object-with-grouped-keys-as-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can do that with pure JS, no need of loadash.
Call the reduce
method of arrays on your input array, and reduce the array to an object, looping over the keys of your inner objs:
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
add a comment |
You can do that with pure JS, no need of loadash.
Call the reduce
method of arrays on your input array, and reduce the array to an object, looping over the keys of your inner objs:
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
add a comment |
You can do that with pure JS, no need of loadash.
Call the reduce
method of arrays on your input array, and reduce the array to an object, looping over the keys of your inner objs:
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
You can do that with pure JS, no need of loadash.
Call the reduce
method of arrays on your input array, and reduce the array to an object, looping over the keys of your inner objs:
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
const input = [{a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {}];
console.log(input.reduce((acc, val) => {
Object.keys(val).forEach(key => {
if(!acc[key]) {
acc[key] = ;
}
acc[key].push(val[key]);
});
return acc;
}, {}));
edited Jan 3 at 3:44


Shidersz
9,8532933
9,8532933
answered Jan 3 at 3:33
quirimmoquirimmo
7,72811536
7,72811536
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
add a comment |
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
1
1
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
I have added more objects to the input set, just to show this works over a generalized set. +1. You can rollback if you dislike this change. I apologize in that case
– Shidersz
Jan 3 at 3:45
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
@Shidersz precious edit! thanks a million :)
– quirimmo
Jan 3 at 3:47
add a comment |
You can use lodash#assignWith to assign all properties their respective values into one object, together with a customizer function to determine how you want to structure the object.
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
Note: To make sure that we don't mutate any of the objects in the data
array, I passed an empty object as the first parameter to act as the destination object.
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
also,_.mergeWith
can be used for the same
– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to uselodash#assignWith
– ryeballar
Jan 3 at 10:20
add a comment |
You can use lodash#assignWith to assign all properties their respective values into one object, together with a customizer function to determine how you want to structure the object.
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
Note: To make sure that we don't mutate any of the objects in the data
array, I passed an empty object as the first parameter to act as the destination object.
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
also,_.mergeWith
can be used for the same
– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to uselodash#assignWith
– ryeballar
Jan 3 at 10:20
add a comment |
You can use lodash#assignWith to assign all properties their respective values into one object, together with a customizer function to determine how you want to structure the object.
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
Note: To make sure that we don't mutate any of the objects in the data
array, I passed an empty object as the first parameter to act as the destination object.
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
You can use lodash#assignWith to assign all properties their respective values into one object, together with a customizer function to determine how you want to structure the object.
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
Note: To make sure that we don't mutate any of the objects in the data
array, I passed an empty object as the first parameter to act as the destination object.
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
const data = [
{ a: 'meow' },
{ a: 'woof', k: 'hey' },
{ k: 'yo', d: 'hehe' },
{ d: 'wazup', q: 'ohoho' }
];
const result = _.assignWith({}, ...data, (v = , s) => v.concat(s));
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>
answered Jan 3 at 4:53
ryeballarryeballar
23.8k94358
23.8k94358
also,_.mergeWith
can be used for the same
– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to uselodash#assignWith
– ryeballar
Jan 3 at 10:20
add a comment |
also,_.mergeWith
can be used for the same
– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to uselodash#assignWith
– ryeballar
Jan 3 at 10:20
also,
_.mergeWith
can be used for the same– Koushik Chatterjee
Jan 3 at 6:41
also,
_.mergeWith
can be used for the same– Koushik Chatterjee
Jan 3 at 6:41
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to use
lodash#assignWith
– ryeballar
Jan 3 at 10:20
Since the OP's situation doesn't deal with recursively assigning values, then I simply opted to use
lodash#assignWith
– ryeballar
Jan 3 at 10:20
add a comment |
I had some issues with typescript and lodash.reduce, this worked.
export function getFuncsObject(funcs): Record<Funcs, Case> {
let f = { ...funcs };
f = lodash.mapValues(f, () => );
return f;
}
export function mockMerge(funcs, mocks: Record<Funcs, Case | null>): Record<Funcs, Case> {
const f = getFuncsObject(funcs);
lodash.each(mocks, (v, k) => {
f[k].push(v);
});
return f;
}
add a comment |
I had some issues with typescript and lodash.reduce, this worked.
export function getFuncsObject(funcs): Record<Funcs, Case> {
let f = { ...funcs };
f = lodash.mapValues(f, () => );
return f;
}
export function mockMerge(funcs, mocks: Record<Funcs, Case | null>): Record<Funcs, Case> {
const f = getFuncsObject(funcs);
lodash.each(mocks, (v, k) => {
f[k].push(v);
});
return f;
}
add a comment |
I had some issues with typescript and lodash.reduce, this worked.
export function getFuncsObject(funcs): Record<Funcs, Case> {
let f = { ...funcs };
f = lodash.mapValues(f, () => );
return f;
}
export function mockMerge(funcs, mocks: Record<Funcs, Case | null>): Record<Funcs, Case> {
const f = getFuncsObject(funcs);
lodash.each(mocks, (v, k) => {
f[k].push(v);
});
return f;
}
I had some issues with typescript and lodash.reduce, this worked.
export function getFuncsObject(funcs): Record<Funcs, Case> {
let f = { ...funcs };
f = lodash.mapValues(f, () => );
return f;
}
export function mockMerge(funcs, mocks: Record<Funcs, Case | null>): Record<Funcs, Case> {
const f = getFuncsObject(funcs);
lodash.each(mocks, (v, k) => {
f[k].push(v);
});
return f;
}
answered Jan 3 at 3:24
ThomasReggiThomasReggi
18.7k46150277
18.7k46150277
add a comment |
add a comment |
One option would be to use two reductions as follows:
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
Not sure if this is clunky compared to your current solution, but it's fairly concise and does not require an external library.
add a comment |
One option would be to use two reductions as follows:
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
Not sure if this is clunky compared to your current solution, but it's fairly concise and does not require an external library.
add a comment |
One option would be to use two reductions as follows:
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
Not sure if this is clunky compared to your current solution, but it's fairly concise and does not require an external library.
One option would be to use two reductions as follows:
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
Not sure if this is clunky compared to your current solution, but it's fairly concise and does not require an external library.
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
const input = [{
a: 'meow'
}, {
a: 'woof'
}, {
b: 'moo'
}];
const result = input
.reduce((itemResult, item) => Object.keys(item)
.reduce((keyResult, key) => ({
...keyResult,
[key]: (keyResult[key] || ).concat(item[key])
}), itemResult), {});
console.log(result)
answered Jan 3 at 3:36


Dacre DennyDacre Denny
14.5k41233
14.5k41233
add a comment |
add a comment |
Without using any external libraries or reduce.
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
add a comment |
Without using any external libraries or reduce.
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
add a comment |
Without using any external libraries or reduce.
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
Without using any external libraries or reduce.
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
const input = [ {a: 'meow'}, {a: 'woof'}, {b: 'hi'}, {a: 'dog', c: 'bye'}, {} ];
let output = {};
input.forEach((inputObj) => {
for(let key in inputObj){
if(!output[ key ]){
output[ key ] = ;
}
output[ key ].push(inputObj[key])
}
});
console.log(output);
answered Jan 3 at 7:31
uniquerockrzuniquerockrz
20629
20629
add a comment |
add a comment |
[{a: 'meow'}, {a: 'woof'}].reduce((m, n) => ({...m, [Object.keys(n)[0]]: (m[Object.keys(n)[0]] && m[Object.keys(n)[0]].concat(Object.values(n)[0])) || .concat(Object.values(n)[0])}), {})
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
add a comment |
[{a: 'meow'}, {a: 'woof'}].reduce((m, n) => ({...m, [Object.keys(n)[0]]: (m[Object.keys(n)[0]] && m[Object.keys(n)[0]].concat(Object.values(n)[0])) || .concat(Object.values(n)[0])}), {})
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
add a comment |
[{a: 'meow'}, {a: 'woof'}].reduce((m, n) => ({...m, [Object.keys(n)[0]]: (m[Object.keys(n)[0]] && m[Object.keys(n)[0]].concat(Object.values(n)[0])) || .concat(Object.values(n)[0])}), {})
[{a: 'meow'}, {a: 'woof'}].reduce((m, n) => ({...m, [Object.keys(n)[0]]: (m[Object.keys(n)[0]] && m[Object.keys(n)[0]].concat(Object.values(n)[0])) || .concat(Object.values(n)[0])}), {})
answered Jan 3 at 3:36


chau giangchau giang
697
697
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
add a comment |
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Why somebody devote my answer, please re-check it?
– chau giang
Jan 3 at 6:09
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
Can you share some explanation for your solution? That looks like really ugly code, and not a "simple way"
– Nico Haase
Jan 3 at 6:28
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54015878%2fsimple-way-to-turn-array-of-objects-into-object-with-grouped-keys-as-arrays%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Care to share the effort? A user with your rep should know the importance of it
– Rajesh
Jan 3 at 7:33