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











up vote
0
down vote

favorite












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


























    up vote
    0
    down vote

    favorite












    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
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      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 at 16:19









      simon

      31




      31
























          8 Answers
          8






          active

          oldest

          votes

















          up vote
          2
          down vote



          accepted










          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




























            up vote
            2
            down vote













                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




























              up vote
              1
              down vote













              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




























                up vote
                1
                down vote
















                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




























                  up vote
                  0
                  down vote













                  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




























                    up vote
                    0
                    down vote













                    You could use the ES6 spread syntax like this:



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





                    share|improve this answer




























                      up vote
                      0
                      down vote













                      Array.concat can do this on its own



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





                      share|improve this answer




























                        up vote
                        0
                        down vote













                        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',
                          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








                          up vote
                          2
                          down vote



                          accepted










                          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

























                            up vote
                            2
                            down vote



                            accepted










                            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























                              up vote
                              2
                              down vote



                              accepted







                              up vote
                              2
                              down vote



                              accepted






                              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 at 16:26









                              brk

                              25.2k31939




                              25.2k31939
























                                  up vote
                                  2
                                  down vote













                                      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

























                                    up vote
                                    2
                                    down vote













                                        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























                                      up vote
                                      2
                                      down vote










                                      up vote
                                      2
                                      down vote









                                          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 at 16:23









                                      bipll

                                      7,8991825




                                      7,8991825






















                                          up vote
                                          1
                                          down vote













                                          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

























                                            up vote
                                            1
                                            down vote













                                            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























                                              up vote
                                              1
                                              down vote










                                              up vote
                                              1
                                              down vote









                                              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 at 16:25









                                              George

                                              4,38711731




                                              4,38711731






















                                                  up vote
                                                  1
                                                  down vote
















                                                  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

























                                                    up vote
                                                    1
                                                    down vote
















                                                    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























                                                      up vote
                                                      1
                                                      down vote










                                                      up vote
                                                      1
                                                      down vote












                                                      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 at 16:28









                                                      Ayon Saha

                                                      30417




                                                      30417






















                                                          up vote
                                                          0
                                                          down vote













                                                          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

























                                                            up vote
                                                            0
                                                            down vote













                                                            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























                                                              up vote
                                                              0
                                                              down vote










                                                              up vote
                                                              0
                                                              down vote









                                                              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 at 16:24









                                                              Christheoreo

                                                              2218




                                                              2218






















                                                                  up vote
                                                                  0
                                                                  down vote













                                                                  You could use the ES6 spread syntax like this:



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





                                                                  share|improve this answer

























                                                                    up vote
                                                                    0
                                                                    down vote













                                                                    You could use the ES6 spread syntax like this:



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





                                                                    share|improve this answer























                                                                      up vote
                                                                      0
                                                                      down vote










                                                                      up vote
                                                                      0
                                                                      down vote









                                                                      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 at 16:25









                                                                      weibenfalk

                                                                      50115




                                                                      50115






















                                                                          up vote
                                                                          0
                                                                          down vote













                                                                          Array.concat can do this on its own



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





                                                                          share|improve this answer

























                                                                            up vote
                                                                            0
                                                                            down vote













                                                                            Array.concat can do this on its own



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





                                                                            share|improve this answer























                                                                              up vote
                                                                              0
                                                                              down vote










                                                                              up vote
                                                                              0
                                                                              down vote









                                                                              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 at 16:26









                                                                              SpeedOfRound

                                                                              603314




                                                                              603314






















                                                                                  up vote
                                                                                  0
                                                                                  down vote













                                                                                  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

























                                                                                    up vote
                                                                                    0
                                                                                    down vote













                                                                                    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























                                                                                      up vote
                                                                                      0
                                                                                      down vote










                                                                                      up vote
                                                                                      0
                                                                                      down vote









                                                                                      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 at 16:32









                                                                                      bugpulver

                                                                                      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.





                                                                                          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                                                                                          Please pay close attention to the following guidance:


                                                                                          • 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







                                                                                          Popular posts from this blog

                                                                                          What visual should I use to simply compare current year value vs last year in Power BI desktop

                                                                                          Alexandru Averescu

                                                                                          Trompette piccolo