Merging multiple arrays of objects of the same length in JavaScript The Next CEO of Stack OverflowLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I correctly clone a JavaScript object?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?

Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?

I believe this to be a fraud - hired, then asked to cash check and send cash as Bitcoin

How can I quit an app using Terminal?

Why didn't Khan get resurrected in the Genesis Explosion?

The King's new dress

Why doesn't a table tennis ball float on the surface? How do we calculate buoyancy here?

How to use tikz in fbox?

Why here is plural "We went to the movies last night."

How do I go from 300 unfinished/half written blog posts, to published posts?

Should I tutor a student who I know has cheated on their homework?

WOW air has ceased operation, can I get my tickets refunded?

Why do remote companies require working in the US?

Is the concept of a "numerable" fiber bundle really useful or an empty generalization?

What is the point of a new vote on May's deal when the indicative votes suggest she will not win?

What does this shorthand mean?

How can I get through very long and very dry, but also very useful technical documents when learning a new tool?

Text adventure game code

When airplanes disconnect from a tanker during air to air refueling, why do they bank so sharply to the right?

Only print output after finding pattern

If the heap is initialized for security, then why is the stack uninitialized?

Why do professional authors make "consistency" mistakes? And how to avoid them?

How to write the block matrix in LaTex?

How do scammers retract money, while you can’t?

How to make a variable always equal to the result of some calculations?



Merging multiple arrays of objects of the same length in JavaScript



The Next CEO of Stack OverflowLength of a JavaScript objectWhat is the most efficient way to deep clone an object in JavaScript?How can I merge properties of two JavaScript objects dynamically?How do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How do I correctly clone a JavaScript object?Loop through an array in JavaScriptHow to check if an object is an array?How do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?










0















Here are examples of the arrays that I need to merge



const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
const images = [image:"...",image:"...",image:"..."];

const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]


I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!










share|improve this question
























  • you can use manual merging - use loop for() and create new objects... simple I think

    – demo
    Mar 8 at 12:50















0















Here are examples of the arrays that I need to merge



const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
const images = [image:"...",image:"...",image:"..."];

const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]


I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!










share|improve this question
























  • you can use manual merging - use loop for() and create new objects... simple I think

    – demo
    Mar 8 at 12:50













0












0








0








Here are examples of the arrays that I need to merge



const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
const images = [image:"...",image:"...",image:"..."];

const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]


I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!










share|improve this question
















Here are examples of the arrays that I need to merge



const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
const images = [image:"...",image:"...",image:"..."];

const merged = [url:"http:/...", image:"...",url:"http:/...", image:"...",url:"http:/...", image:"..."]


I have tried Object.assign(,urls, images). That ends up just deleting urls in that instance because it does not deep copy. The keys for each object in the array are the same!







javascript ecmascript-6






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 20 at 15:30









Dream Hunter - hashADH

2,14511635




2,14511635










asked Mar 8 at 12:48









AlexMcGAlexMcG

801212




801212












  • you can use manual merging - use loop for() and create new objects... simple I think

    – demo
    Mar 8 at 12:50

















  • you can use manual merging - use loop for() and create new objects... simple I think

    – demo
    Mar 8 at 12:50
















you can use manual merging - use loop for() and create new objects... simple I think

– demo
Mar 8 at 12:50





you can use manual merging - use loop for() and create new objects... simple I think

– demo
Mar 8 at 12:50












4 Answers
4






active

oldest

votes


















2














If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
Actually, you should use Object.assign if you want to merge an object with a more generic way.






const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
const images = [image:"...",image:"...",image:"..."];

const results = urls.map((url, index) =>
Object.assign(, url, images[index])
);

console.log(results)





ES6



you can use ES6 instead of Object.assign.



const results = urls.map((url, index) => 
(...url, ...images[index])
);





share|improve this answer
































    1














    You could map over one of the arrays and use the index to get the other array's item:






    const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
    const images = [image:"0...",image:"1...",image:"2..."];

    const newArray = urls.map((url, i) => (...url, ...images[i] ) )

    console.log(newArray)





    Or you could use Array.from like this:






    const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
    images = [image:"0...",image:"1...",image:"2..."];

    const length = urls.length

    const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
    console.log(newArray)








    share|improve this answer
































      0














      You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.



      Here's an example:






      const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
      const images = [image:"...",image:"...",image:"..."];

      console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





      Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.






      share|improve this answer






























        0














        You could reduce the arrays and assign the object to the object with the same index.



        This works for an arbitrary count of arrays.






        const
        urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
        images = [ image: "..." , image: "..." , image: "..." ],
        result = [urls, images]
        .reduce((r, a) => (
        a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
        r
        ), []);

        console.log(result);

        .as-console-wrapper max-height: 100% !important; top: 0; 








        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',
          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%2f55063572%2fmerging-multiple-arrays-of-objects-of-the-same-length-in-javascript%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          4 Answers
          4






          active

          oldest

          votes








          4 Answers
          4






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
          Actually, you should use Object.assign if you want to merge an object with a more generic way.






          const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
          const images = [image:"...",image:"...",image:"..."];

          const results = urls.map((url, index) =>
          Object.assign(, url, images[index])
          );

          console.log(results)





          ES6



          you can use ES6 instead of Object.assign.



          const results = urls.map((url, index) => 
          (...url, ...images[index])
          );





          share|improve this answer





























            2














            If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
            Actually, you should use Object.assign if you want to merge an object with a more generic way.






            const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
            const images = [image:"...",image:"...",image:"..."];

            const results = urls.map((url, index) =>
            Object.assign(, url, images[index])
            );

            console.log(results)





            ES6



            you can use ES6 instead of Object.assign.



            const results = urls.map((url, index) => 
            (...url, ...images[index])
            );





            share|improve this answer



























              2












              2








              2







              If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
              Actually, you should use Object.assign if you want to merge an object with a more generic way.






              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
              const images = [image:"...",image:"...",image:"..."];

              const results = urls.map((url, index) =>
              Object.assign(, url, images[index])
              );

              console.log(results)





              ES6



              you can use ES6 instead of Object.assign.



              const results = urls.map((url, index) => 
              (...url, ...images[index])
              );





              share|improve this answer















              If you are certainly sure they are both equal size, you definitely can run through one of them with Array.prototype.map method.
              Actually, you should use Object.assign if you want to merge an object with a more generic way.






              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
              const images = [image:"...",image:"...",image:"..."];

              const results = urls.map((url, index) =>
              Object.assign(, url, images[index])
              );

              console.log(results)





              ES6



              you can use ES6 instead of Object.assign.



              const results = urls.map((url, index) => 
              (...url, ...images[index])
              );





              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
              const images = [image:"...",image:"...",image:"..."];

              const results = urls.map((url, index) =>
              Object.assign(, url, images[index])
              );

              console.log(results)





              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
              const images = [image:"...",image:"...",image:"..."];

              const results = urls.map((url, index) =>
              Object.assign(, url, images[index])
              );

              console.log(results)






              share|improve this answer














              share|improve this answer



              share|improve this answer








              edited Mar 8 at 13:11

























              answered Mar 8 at 12:53









              Lidor AvitanLidor Avitan

              721415




              721415























                  1














                  You could map over one of the arrays and use the index to get the other array's item:






                  const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                  const images = [image:"0...",image:"1...",image:"2..."];

                  const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                  console.log(newArray)





                  Or you could use Array.from like this:






                  const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                  images = [image:"0...",image:"1...",image:"2..."];

                  const length = urls.length

                  const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                  console.log(newArray)








                  share|improve this answer





























                    1














                    You could map over one of the arrays and use the index to get the other array's item:






                    const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                    const images = [image:"0...",image:"1...",image:"2..."];

                    const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                    console.log(newArray)





                    Or you could use Array.from like this:






                    const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                    images = [image:"0...",image:"1...",image:"2..."];

                    const length = urls.length

                    const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                    console.log(newArray)








                    share|improve this answer



























                      1












                      1








                      1







                      You could map over one of the arrays and use the index to get the other array's item:






                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                      const images = [image:"0...",image:"1...",image:"2..."];

                      const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                      console.log(newArray)





                      Or you could use Array.from like this:






                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                      images = [image:"0...",image:"1...",image:"2..."];

                      const length = urls.length

                      const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                      console.log(newArray)








                      share|improve this answer















                      You could map over one of the arrays and use the index to get the other array's item:






                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                      const images = [image:"0...",image:"1...",image:"2..."];

                      const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                      console.log(newArray)





                      Or you could use Array.from like this:






                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                      images = [image:"0...",image:"1...",image:"2..."];

                      const length = urls.length

                      const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                      console.log(newArray)








                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                      const images = [image:"0...",image:"1...",image:"2..."];

                      const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                      console.log(newArray)





                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."]; 
                      const images = [image:"0...",image:"1...",image:"2..."];

                      const newArray = urls.map((url, i) => (...url, ...images[i] ) )

                      console.log(newArray)





                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                      images = [image:"0...",image:"1...",image:"2..."];

                      const length = urls.length

                      const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                      console.log(newArray)





                      const urls = [url:"http:/0...",url:"http:/1...",url:"http:/2..."],
                      images = [image:"0...",image:"1...",image:"2..."];

                      const length = urls.length

                      const newArray = Array.from( length , (_, i) => ( ...urls[i], ...images[i] ) )
                      console.log(newArray)






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited Mar 8 at 13:26

























                      answered Mar 8 at 12:52









                      adigaadiga

                      12k62545




                      12k62545





















                          0














                          You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.



                          Here's an example:






                          const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                          const images = [image:"...",image:"...",image:"..."];

                          console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





                          Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.






                          share|improve this answer



























                            0














                            You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.



                            Here's an example:






                            const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                            const images = [image:"...",image:"...",image:"..."];

                            console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





                            Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.






                            share|improve this answer

























                              0












                              0








                              0







                              You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.



                              Here's an example:






                              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                              const images = [image:"...",image:"...",image:"..."];

                              console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





                              Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.






                              share|improve this answer













                              You could iterate over one of the arrays using Array.prototype.map, and using the index provided to the callback function, you can get corresponding value in the other array.



                              Here's an example:






                              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                              const images = [image:"...",image:"...",image:"..."];

                              console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





                              Here the parameters passed into the callback function are t: the item from urls array and id: index of t in urls array.






                              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                              const images = [image:"...",image:"...",image:"..."];

                              console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));





                              const urls = [url:"http:/...",url:"http:/...",url:"http:/..."]; 
                              const images = [image:"...",image:"...",image:"..."];

                              console.log(urls.map((t, id) => ( image: images[id].image, url: t.url )));






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Mar 8 at 12:51









                              NisargNisarg

                              11.2k52341




                              11.2k52341





















                                  0














                                  You could reduce the arrays and assign the object to the object with the same index.



                                  This works for an arbitrary count of arrays.






                                  const
                                  urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                  images = [ image: "..." , image: "..." , image: "..." ],
                                  result = [urls, images]
                                  .reduce((r, a) => (
                                  a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                  r
                                  ), []);

                                  console.log(result);

                                  .as-console-wrapper max-height: 100% !important; top: 0; 








                                  share|improve this answer



























                                    0














                                    You could reduce the arrays and assign the object to the object with the same index.



                                    This works for an arbitrary count of arrays.






                                    const
                                    urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                    images = [ image: "..." , image: "..." , image: "..." ],
                                    result = [urls, images]
                                    .reduce((r, a) => (
                                    a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                    r
                                    ), []);

                                    console.log(result);

                                    .as-console-wrapper max-height: 100% !important; top: 0; 








                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      You could reduce the arrays and assign the object to the object with the same index.



                                      This works for an arbitrary count of arrays.






                                      const
                                      urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                      images = [ image: "..." , image: "..." , image: "..." ],
                                      result = [urls, images]
                                      .reduce((r, a) => (
                                      a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                      r
                                      ), []);

                                      console.log(result);

                                      .as-console-wrapper max-height: 100% !important; top: 0; 








                                      share|improve this answer













                                      You could reduce the arrays and assign the object to the object with the same index.



                                      This works for an arbitrary count of arrays.






                                      const
                                      urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                      images = [ image: "..." , image: "..." , image: "..." ],
                                      result = [urls, images]
                                      .reduce((r, a) => (
                                      a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                      r
                                      ), []);

                                      console.log(result);

                                      .as-console-wrapper max-height: 100% !important; top: 0; 








                                      const
                                      urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                      images = [ image: "..." , image: "..." , image: "..." ],
                                      result = [urls, images]
                                      .reduce((r, a) => (
                                      a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                      r
                                      ), []);

                                      console.log(result);

                                      .as-console-wrapper max-height: 100% !important; top: 0; 





                                      const
                                      urls = [url:"http:/...", url: "http:/..." , url: "http:/..." ],
                                      images = [ image: "..." , image: "..." , image: "..." ],
                                      result = [urls, images]
                                      .reduce((r, a) => (
                                      a.forEach((o, i) => Object.assign(r[i] = r[i] || , o)),
                                      r
                                      ), []);

                                      console.log(result);

                                      .as-console-wrapper max-height: 100% !important; top: 0; 






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Mar 8 at 12:59









                                      Nina ScholzNina Scholz

                                      194k15107178




                                      194k15107178



























                                          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%2f55063572%2fmerging-multiple-arrays-of-objects-of-the-same-length-in-javascript%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

                                          How to get text form Clipboard with JavaScript in Firefox 56?How to validate an email address in JavaScript?How do JavaScript closures work?How do I remove a property from a JavaScript object?How do you get a timestamp in JavaScript?How do I copy to the clipboard in JavaScript?How do I include a JavaScript file in another JavaScript file?Get the current URL with JavaScript?How to replace all occurrences of a string in JavaScriptHow to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

                                          Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

                                          List of MPs elected to the English parliament in 1640 (April) Contents List of constituencies and members See also Notes References Navigation menueNational Archives – The Glynde Place ArchivesCobbett's Parliamentary history of England, from the Norman Conquest in 1066 to the year 1803'Aldermen in Parliament', The Aldermen of the City of London: Temp. Henry III – 1912onepage&q&f&#61, false 229