How can you merge objects in array of objects?

The name of the pictureThe name of the pictureThe name of the pictureClash 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?










share|improve this question



















  • 2




    is every object in arrayOfObjects guaranteed to have the same properties?
    – jtate
    2 hours ago














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?










share|improve this question



















  • 2




    is every object in arrayOfObjects guaranteed to have the same properties?
    – jtate
    2 hours ago












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?










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 9 mins ago









Constantin Chirila

16911




16911










asked 2 hours ago









Rafonix

476




476







  • 2




    is every object in arrayOfObjects guaranteed to have the same properties?
    – jtate
    2 hours ago












  • 2




    is every object in arrayOfObjects 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












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








share|improve this answer



























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








    share|improve this answer



























      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)





      share|improve this answer



























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





        share|improve this answer



























          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






          share|improve this answer






















          • 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

















          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>








          share|improve this answer





























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








            share|improve this answer






















              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',
              convertImagesToLinks: true,
              noModals: false,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              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%2f52761212%2fhow-can-you-merge-objects-in-array-of-objects%23new-answer', 'question_page');

              );

              Post as a guest






























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








              share|improve this answer
























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








                share|improve this answer






















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








                  share|improve this answer












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






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 hours ago









                  ssc-hrep3

                  4,67521551




                  4,67521551






















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








                      share|improve this answer
























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








                        share|improve this answer






















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








                          share|improve this answer












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






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered 2 hours ago









                          Nina Scholz

                          163k1280141




                          163k1280141




















                              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)





                              share|improve this answer
























                                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)





                                share|improve this answer






















                                  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)





                                  share|improve this answer












                                  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)






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  answered 2 hours ago









                                  georg

                                  140k31191287




                                  140k31191287




















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





                                      share|improve this answer
























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





                                        share|improve this answer






















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





                                          share|improve this answer












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






                                          share|improve this answer












                                          share|improve this answer



                                          share|improve this answer










                                          answered 2 hours ago









                                          Robin Zigmond

                                          88839




                                          88839




















                                              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






                                              share|improve this answer






















                                              • 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














                                              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






                                              share|improve this answer






















                                              • 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












                                              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






                                              share|improve this answer














                                              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>






                                              share|improve this answer














                                              share|improve this answer



                                              share|improve this answer








                                              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
















                                              • 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










                                              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>








                                              share|improve this answer


























                                                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>








                                                share|improve this answer
























                                                  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>








                                                  share|improve this answer














                                                  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>






                                                  share|improve this answer














                                                  share|improve this answer



                                                  share|improve this answer








                                                  edited 1 hour ago

























                                                  answered 1 hour ago









                                                  ibrahim mahrir

                                                  20.2k41442




                                                  20.2k41442




















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








                                                      share|improve this answer


























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








                                                        share|improve this answer
























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








                                                          share|improve this answer














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






                                                          share|improve this answer














                                                          share|improve this answer



                                                          share|improve this answer








                                                          edited 1 hour ago

























                                                          answered 1 hour ago









                                                          protoproto

                                                          47336




                                                          47336



























                                                               

                                                              draft saved


                                                              draft discarded















































                                                               


                                                              draft saved


                                                              draft discarded














                                                              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













































































                                                              Comments

                                                              Popular posts from this blog

                                                              Long meetings (6-7 hours a day): Being “babysat” by supervisor

                                                              What does second last employer means? [closed]

                                                              One-line joke