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;
}







2















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']}









share|improve this question

























  • Care to share the effort? A user with your rep should know the importance of it

    – Rajesh
    Jan 3 at 7:33


















2















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']}









share|improve this question

























  • Care to share the effort? A user with your rep should know the importance of it

    – Rajesh
    Jan 3 at 7:33














2












2








2








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']}









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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



















  • 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












6 Answers
6






active

oldest

votes


















2














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;
}, {}));








share|improve this answer





















  • 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



















2














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>








share|improve this answer
























  • 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





















0














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;
}





share|improve this answer































    0














    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.






    share|improve this answer































      0














      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);








      share|improve this answer































        -1














            [{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])}), {})





        share|improve this answer
























        • 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












        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
        });


        }
        });














        draft saved

        draft discarded


















        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









        2














        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;
        }, {}));








        share|improve this answer





















        • 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
















        2














        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;
        }, {}));








        share|improve this answer





















        • 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














        2












        2








        2







        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;
        }, {}));








        share|improve this answer















        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;
        }, {}));






        share|improve this answer














        share|improve this answer



        share|improve this answer








        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














        • 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













        2














        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>








        share|improve this answer
























        • 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


















        2














        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>








        share|improve this answer
























        • 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
















        2












        2








        2







        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>








        share|improve this answer













        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>






        share|improve this answer












        share|improve this answer



        share|improve this answer










        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 use lodash#assignWith

          – ryeballar
          Jan 3 at 10:20





















        • 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



















        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













        0














        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;
        }





        share|improve this answer




























          0














          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;
          }





          share|improve this answer


























            0












            0








            0







            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;
            }





            share|improve this answer













            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;
            }






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 3 at 3:24









            ThomasReggiThomasReggi

            18.7k46150277




            18.7k46150277























                0














                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.






                share|improve this answer




























                  0














                  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.






                  share|improve this answer


























                    0












                    0








                    0







                    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.






                    share|improve this answer













                    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)






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 3 at 3:36









                    Dacre DennyDacre Denny

                    14.5k41233




                    14.5k41233























                        0














                        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);








                        share|improve this answer




























                          0














                          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);








                          share|improve this answer


























                            0












                            0








                            0







                            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);








                            share|improve this answer













                            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);






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 3 at 7:31









                            uniquerockrzuniquerockrz

                            20629




                            20629























                                -1














                                    [{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])}), {})





                                share|improve this answer
























                                • 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
















                                -1














                                    [{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])}), {})





                                share|improve this answer
























                                • 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














                                -1












                                -1








                                -1







                                    [{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])}), {})





                                share|improve this answer













                                    [{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])}), {})






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                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



















                                • 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


















                                draft saved

                                draft discarded




















































                                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.




                                draft saved


                                draft discarded














                                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





















































                                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







                                Popular posts from this blog

                                MongoDB - Not Authorized To Execute Command

                                How to fix TextFormField cause rebuild widget in Flutter

                                in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith