Javascript: Turn a nested array into a single array using a nested for loop












0















This is just a simple javascript exercise that I'm working on.



I'm trying to convert this array...



var array = [
[1,2],
[3,4],
[5,6]
];


into...



array = [1, 2, 3, 4, 5, 6];


by using this nested for loop.



var series;
var storage = ;
for (var i = 0; i < array.length; i++) {
for (var j = 0; j < array[i].length; j++) {
series = array[i][j];
for (var k = 0; k < 6; k++) {
storage[k] = series;
};
};
};

console.log(storage);


With an output of...



//Output: [6, 6, 6, 6, 6, 6]


Why is this the output and how can I fix it?










share|improve this question



























    0















    This is just a simple javascript exercise that I'm working on.



    I'm trying to convert this array...



    var array = [
    [1,2],
    [3,4],
    [5,6]
    ];


    into...



    array = [1, 2, 3, 4, 5, 6];


    by using this nested for loop.



    var series;
    var storage = ;
    for (var i = 0; i < array.length; i++) {
    for (var j = 0; j < array[i].length; j++) {
    series = array[i][j];
    for (var k = 0; k < 6; k++) {
    storage[k] = series;
    };
    };
    };

    console.log(storage);


    With an output of...



    //Output: [6, 6, 6, 6, 6, 6]


    Why is this the output and how can I fix it?










    share|improve this question

























      0












      0








      0








      This is just a simple javascript exercise that I'm working on.



      I'm trying to convert this array...



      var array = [
      [1,2],
      [3,4],
      [5,6]
      ];


      into...



      array = [1, 2, 3, 4, 5, 6];


      by using this nested for loop.



      var series;
      var storage = ;
      for (var i = 0; i < array.length; i++) {
      for (var j = 0; j < array[i].length; j++) {
      series = array[i][j];
      for (var k = 0; k < 6; k++) {
      storage[k] = series;
      };
      };
      };

      console.log(storage);


      With an output of...



      //Output: [6, 6, 6, 6, 6, 6]


      Why is this the output and how can I fix it?










      share|improve this question














      This is just a simple javascript exercise that I'm working on.



      I'm trying to convert this array...



      var array = [
      [1,2],
      [3,4],
      [5,6]
      ];


      into...



      array = [1, 2, 3, 4, 5, 6];


      by using this nested for loop.



      var series;
      var storage = ;
      for (var i = 0; i < array.length; i++) {
      for (var j = 0; j < array[i].length; j++) {
      series = array[i][j];
      for (var k = 0; k < 6; k++) {
      storage[k] = series;
      };
      };
      };

      console.log(storage);


      With an output of...



      //Output: [6, 6, 6, 6, 6, 6]


      Why is this the output and how can I fix it?







      javascript loops for-loop nested






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 16:19









      simonsimon

      31




      31
























          8 Answers
          8






          active

          oldest

          votes


















          2














          for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element






          var array = [
          [1, 2],
          [3, 4],
          [5, 6]
          ];


          var series;
          var storage = ;
          for (var i = 0; i < array.length; i++) {
          for (var j = 0; j < array[i].length; j++) {
          storage.push(array[i][j])
          };
          };

          console.log(storage);








          share|improve this answer































            2














                series = array[i][j];
            for (var k = 0; k < 6; k++) {
            storage[k] = series;
            };


            Seriously, here you set the same value to each element of the resulting array.



            You probably need something like



            for(let x of array) {
            for(let y of x) {
            storage.push(y)
            }
            }


            Or, if your JS machine is experimental enough, simply



            var storage = array.flat()





            share|improve this answer































              1














              You can use a mix of reduce and concat to achieve what you want in one line






              var array = [
              [1, 2],
              [3, 4],
              [5, 6]
              ];

              console.log(array.reduce((a, v) => a.concat(v), ));





              As for why your code didn't work, it's mainly down to this bit



              for (var k = 0; k < 6; k++) {
              storage[k] = series;
              };


              It would overwrite everything in the array with the last value of series, which in your case would be 6






              share|improve this answer































                1














                Array.concat can do this on its own



                var merged = .concat.apply(, array);





                share|improve this answer































                  1

















                  var array = [
                  [1,2],
                  [3,4],
                  [5,6]
                  ];
                  var newArray = ;
                  for (let i = 0; i < array.length; i++) {
                  newArray = newArray.concat(array[i]);

                  }
                  console.log(newArray)








                  share|improve this answer































                    0














                    You can use array.flat()



                    Reference : flatten



                    var array = [
                    [1,2],
                    [3,4],
                    [5,6]
                    ];
                    array.flat();
                    // 1,2,3,4,5,6....





                    share|improve this answer































                      0














                      You could use the ES6 spread syntax like this:



                      for (let element of array){
                      storage.push( ... el )
                      }





                      share|improve this answer































                        0














                        storage = ;
                        for(var i=0; i<array.length; i++)
                        storage = storage.concat(array[i]);


                        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat






                        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%2f53434913%2fjavascript-turn-a-nested-array-into-a-single-array-using-a-nested-for-loop%23new-answer', 'question_page');
                          }
                          );

                          Post as a guest















                          Required, but never shown

























                          8 Answers
                          8






                          active

                          oldest

                          votes








                          8 Answers
                          8






                          active

                          oldest

                          votes









                          active

                          oldest

                          votes






                          active

                          oldest

                          votes









                          2














                          for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element






                          var array = [
                          [1, 2],
                          [3, 4],
                          [5, 6]
                          ];


                          var series;
                          var storage = ;
                          for (var i = 0; i < array.length; i++) {
                          for (var j = 0; j < array[i].length; j++) {
                          storage.push(array[i][j])
                          };
                          };

                          console.log(storage);








                          share|improve this answer




























                            2














                            for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element






                            var array = [
                            [1, 2],
                            [3, 4],
                            [5, 6]
                            ];


                            var series;
                            var storage = ;
                            for (var i = 0; i < array.length; i++) {
                            for (var j = 0; j < array[i].length; j++) {
                            storage.push(array[i][j])
                            };
                            };

                            console.log(storage);








                            share|improve this answer


























                              2












                              2








                              2







                              for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element






                              var array = [
                              [1, 2],
                              [3, 4],
                              [5, 6]
                              ];


                              var series;
                              var storage = ;
                              for (var i = 0; i < array.length; i++) {
                              for (var j = 0; j < array[i].length; j++) {
                              storage.push(array[i][j])
                              };
                              };

                              console.log(storage);








                              share|improve this answer













                              for (var k = 0; k < 6; k++) { is not required . array[i] will be each of element inside main array , so iterating over array[i] you can access each of the element






                              var array = [
                              [1, 2],
                              [3, 4],
                              [5, 6]
                              ];


                              var series;
                              var storage = ;
                              for (var i = 0; i < array.length; i++) {
                              for (var j = 0; j < array[i].length; j++) {
                              storage.push(array[i][j])
                              };
                              };

                              console.log(storage);








                              var array = [
                              [1, 2],
                              [3, 4],
                              [5, 6]
                              ];


                              var series;
                              var storage = ;
                              for (var i = 0; i < array.length; i++) {
                              for (var j = 0; j < array[i].length; j++) {
                              storage.push(array[i][j])
                              };
                              };

                              console.log(storage);





                              var array = [
                              [1, 2],
                              [3, 4],
                              [5, 6]
                              ];


                              var series;
                              var storage = ;
                              for (var i = 0; i < array.length; i++) {
                              for (var j = 0; j < array[i].length; j++) {
                              storage.push(array[i][j])
                              };
                              };

                              console.log(storage);






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 22 '18 at 16:26









                              brkbrk

                              27.9k32142




                              27.9k32142

























                                  2














                                      series = array[i][j];
                                  for (var k = 0; k < 6; k++) {
                                  storage[k] = series;
                                  };


                                  Seriously, here you set the same value to each element of the resulting array.



                                  You probably need something like



                                  for(let x of array) {
                                  for(let y of x) {
                                  storage.push(y)
                                  }
                                  }


                                  Or, if your JS machine is experimental enough, simply



                                  var storage = array.flat()





                                  share|improve this answer




























                                    2














                                        series = array[i][j];
                                    for (var k = 0; k < 6; k++) {
                                    storage[k] = series;
                                    };


                                    Seriously, here you set the same value to each element of the resulting array.



                                    You probably need something like



                                    for(let x of array) {
                                    for(let y of x) {
                                    storage.push(y)
                                    }
                                    }


                                    Or, if your JS machine is experimental enough, simply



                                    var storage = array.flat()





                                    share|improve this answer


























                                      2












                                      2








                                      2







                                          series = array[i][j];
                                      for (var k = 0; k < 6; k++) {
                                      storage[k] = series;
                                      };


                                      Seriously, here you set the same value to each element of the resulting array.



                                      You probably need something like



                                      for(let x of array) {
                                      for(let y of x) {
                                      storage.push(y)
                                      }
                                      }


                                      Or, if your JS machine is experimental enough, simply



                                      var storage = array.flat()





                                      share|improve this answer













                                          series = array[i][j];
                                      for (var k = 0; k < 6; k++) {
                                      storage[k] = series;
                                      };


                                      Seriously, here you set the same value to each element of the resulting array.



                                      You probably need something like



                                      for(let x of array) {
                                      for(let y of x) {
                                      storage.push(y)
                                      }
                                      }


                                      Or, if your JS machine is experimental enough, simply



                                      var storage = array.flat()






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 22 '18 at 16:23









                                      bipllbipll

                                      8,2361927




                                      8,2361927























                                          1














                                          You can use a mix of reduce and concat to achieve what you want in one line






                                          var array = [
                                          [1, 2],
                                          [3, 4],
                                          [5, 6]
                                          ];

                                          console.log(array.reduce((a, v) => a.concat(v), ));





                                          As for why your code didn't work, it's mainly down to this bit



                                          for (var k = 0; k < 6; k++) {
                                          storage[k] = series;
                                          };


                                          It would overwrite everything in the array with the last value of series, which in your case would be 6






                                          share|improve this answer




























                                            1














                                            You can use a mix of reduce and concat to achieve what you want in one line






                                            var array = [
                                            [1, 2],
                                            [3, 4],
                                            [5, 6]
                                            ];

                                            console.log(array.reduce((a, v) => a.concat(v), ));





                                            As for why your code didn't work, it's mainly down to this bit



                                            for (var k = 0; k < 6; k++) {
                                            storage[k] = series;
                                            };


                                            It would overwrite everything in the array with the last value of series, which in your case would be 6






                                            share|improve this answer


























                                              1












                                              1








                                              1







                                              You can use a mix of reduce and concat to achieve what you want in one line






                                              var array = [
                                              [1, 2],
                                              [3, 4],
                                              [5, 6]
                                              ];

                                              console.log(array.reduce((a, v) => a.concat(v), ));





                                              As for why your code didn't work, it's mainly down to this bit



                                              for (var k = 0; k < 6; k++) {
                                              storage[k] = series;
                                              };


                                              It would overwrite everything in the array with the last value of series, which in your case would be 6






                                              share|improve this answer













                                              You can use a mix of reduce and concat to achieve what you want in one line






                                              var array = [
                                              [1, 2],
                                              [3, 4],
                                              [5, 6]
                                              ];

                                              console.log(array.reduce((a, v) => a.concat(v), ));





                                              As for why your code didn't work, it's mainly down to this bit



                                              for (var k = 0; k < 6; k++) {
                                              storage[k] = series;
                                              };


                                              It would overwrite everything in the array with the last value of series, which in your case would be 6






                                              var array = [
                                              [1, 2],
                                              [3, 4],
                                              [5, 6]
                                              ];

                                              console.log(array.reduce((a, v) => a.concat(v), ));





                                              var array = [
                                              [1, 2],
                                              [3, 4],
                                              [5, 6]
                                              ];

                                              console.log(array.reduce((a, v) => a.concat(v), ));






                                              share|improve this answer












                                              share|improve this answer



                                              share|improve this answer










                                              answered Nov 22 '18 at 16:25









                                              GeorgeGeorge

                                              4,50811732




                                              4,50811732























                                                  1














                                                  Array.concat can do this on its own



                                                  var merged = .concat.apply(, array);





                                                  share|improve this answer




























                                                    1














                                                    Array.concat can do this on its own



                                                    var merged = .concat.apply(, array);





                                                    share|improve this answer


























                                                      1












                                                      1








                                                      1







                                                      Array.concat can do this on its own



                                                      var merged = .concat.apply(, array);





                                                      share|improve this answer













                                                      Array.concat can do this on its own



                                                      var merged = .concat.apply(, array);






                                                      share|improve this answer












                                                      share|improve this answer



                                                      share|improve this answer










                                                      answered Nov 22 '18 at 16:26









                                                      SpeedOfRoundSpeedOfRound

                                                      629314




                                                      629314























                                                          1

















                                                          var array = [
                                                          [1,2],
                                                          [3,4],
                                                          [5,6]
                                                          ];
                                                          var newArray = ;
                                                          for (let i = 0; i < array.length; i++) {
                                                          newArray = newArray.concat(array[i]);

                                                          }
                                                          console.log(newArray)








                                                          share|improve this answer




























                                                            1

















                                                            var array = [
                                                            [1,2],
                                                            [3,4],
                                                            [5,6]
                                                            ];
                                                            var newArray = ;
                                                            for (let i = 0; i < array.length; i++) {
                                                            newArray = newArray.concat(array[i]);

                                                            }
                                                            console.log(newArray)








                                                            share|improve this answer


























                                                              1












                                                              1








                                                              1










                                                              var array = [
                                                              [1,2],
                                                              [3,4],
                                                              [5,6]
                                                              ];
                                                              var newArray = ;
                                                              for (let i = 0; i < array.length; i++) {
                                                              newArray = newArray.concat(array[i]);

                                                              }
                                                              console.log(newArray)








                                                              share|improve this answer
















                                                              var array = [
                                                              [1,2],
                                                              [3,4],
                                                              [5,6]
                                                              ];
                                                              var newArray = ;
                                                              for (let i = 0; i < array.length; i++) {
                                                              newArray = newArray.concat(array[i]);

                                                              }
                                                              console.log(newArray)








                                                              var array = [
                                                              [1,2],
                                                              [3,4],
                                                              [5,6]
                                                              ];
                                                              var newArray = ;
                                                              for (let i = 0; i < array.length; i++) {
                                                              newArray = newArray.concat(array[i]);

                                                              }
                                                              console.log(newArray)





                                                              var array = [
                                                              [1,2],
                                                              [3,4],
                                                              [5,6]
                                                              ];
                                                              var newArray = ;
                                                              for (let i = 0; i < array.length; i++) {
                                                              newArray = newArray.concat(array[i]);

                                                              }
                                                              console.log(newArray)






                                                              share|improve this answer












                                                              share|improve this answer



                                                              share|improve this answer










                                                              answered Nov 22 '18 at 16:28









                                                              Ayon SahaAyon Saha

                                                              30427




                                                              30427























                                                                  0














                                                                  You can use array.flat()



                                                                  Reference : flatten



                                                                  var array = [
                                                                  [1,2],
                                                                  [3,4],
                                                                  [5,6]
                                                                  ];
                                                                  array.flat();
                                                                  // 1,2,3,4,5,6....





                                                                  share|improve this answer




























                                                                    0














                                                                    You can use array.flat()



                                                                    Reference : flatten



                                                                    var array = [
                                                                    [1,2],
                                                                    [3,4],
                                                                    [5,6]
                                                                    ];
                                                                    array.flat();
                                                                    // 1,2,3,4,5,6....





                                                                    share|improve this answer


























                                                                      0












                                                                      0








                                                                      0







                                                                      You can use array.flat()



                                                                      Reference : flatten



                                                                      var array = [
                                                                      [1,2],
                                                                      [3,4],
                                                                      [5,6]
                                                                      ];
                                                                      array.flat();
                                                                      // 1,2,3,4,5,6....





                                                                      share|improve this answer













                                                                      You can use array.flat()



                                                                      Reference : flatten



                                                                      var array = [
                                                                      [1,2],
                                                                      [3,4],
                                                                      [5,6]
                                                                      ];
                                                                      array.flat();
                                                                      // 1,2,3,4,5,6....






                                                                      share|improve this answer












                                                                      share|improve this answer



                                                                      share|improve this answer










                                                                      answered Nov 22 '18 at 16:24









                                                                      ChristheoreoChristheoreo

                                                                      2269




                                                                      2269























                                                                          0














                                                                          You could use the ES6 spread syntax like this:



                                                                          for (let element of array){
                                                                          storage.push( ... el )
                                                                          }





                                                                          share|improve this answer




























                                                                            0














                                                                            You could use the ES6 spread syntax like this:



                                                                            for (let element of array){
                                                                            storage.push( ... el )
                                                                            }





                                                                            share|improve this answer


























                                                                              0












                                                                              0








                                                                              0







                                                                              You could use the ES6 spread syntax like this:



                                                                              for (let element of array){
                                                                              storage.push( ... el )
                                                                              }





                                                                              share|improve this answer













                                                                              You could use the ES6 spread syntax like this:



                                                                              for (let element of array){
                                                                              storage.push( ... el )
                                                                              }






                                                                              share|improve this answer












                                                                              share|improve this answer



                                                                              share|improve this answer










                                                                              answered Nov 22 '18 at 16:25









                                                                              weibenfalkweibenfalk

                                                                              54617




                                                                              54617























                                                                                  0














                                                                                  storage = ;
                                                                                  for(var i=0; i<array.length; i++)
                                                                                  storage = storage.concat(array[i]);


                                                                                  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat






                                                                                  share|improve this answer




























                                                                                    0














                                                                                    storage = ;
                                                                                    for(var i=0; i<array.length; i++)
                                                                                    storage = storage.concat(array[i]);


                                                                                    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat






                                                                                    share|improve this answer


























                                                                                      0












                                                                                      0








                                                                                      0







                                                                                      storage = ;
                                                                                      for(var i=0; i<array.length; i++)
                                                                                      storage = storage.concat(array[i]);


                                                                                      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat






                                                                                      share|improve this answer













                                                                                      storage = ;
                                                                                      for(var i=0; i<array.length; i++)
                                                                                      storage = storage.concat(array[i]);


                                                                                      https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat







                                                                                      share|improve this answer












                                                                                      share|improve this answer



                                                                                      share|improve this answer










                                                                                      answered Nov 22 '18 at 16:32









                                                                                      bugpulverbugpulver

                                                                                      85




                                                                                      85






























                                                                                          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%2f53434913%2fjavascript-turn-a-nested-array-into-a-single-array-using-a-nested-for-loop%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