How to splice an array out of a nested array - javascript












2















I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1) should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:



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

var item = items.splice(0,1); // should slice first array out of items array

console.log(item); //should log [1,2], instead logs [[1,2]]


However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]. What in the world am I missing!?










share|improve this question




















  • 1





    No need for splice. Just use items[0] instead.

    – PHPglue
    Sep 1 '15 at 2:22











  • Well I want to alter the original array and then chose a value based on an element of the spliced array.

    – Afs35mm
    Sep 1 '15 at 19:24
















2















I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1) should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:



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

var item = items.splice(0,1); // should slice first array out of items array

console.log(item); //should log [1,2], instead logs [[1,2]]


However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]. What in the world am I missing!?










share|improve this question




















  • 1





    No need for splice. Just use items[0] instead.

    – PHPglue
    Sep 1 '15 at 2:22











  • Well I want to alter the original array and then chose a value based on an element of the spliced array.

    – Afs35mm
    Sep 1 '15 at 19:24














2












2








2


1






I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1) should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:



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

var item = items.splice(0,1); // should slice first array out of items array

console.log(item); //should log [1,2], instead logs [[1,2]]


However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]. What in the world am I missing!?










share|improve this question
















I am attempting to splice a nested array out of its parent array. Consider the following array. items.splice(0,1) should give me the first nested array([1,2]), however it seems to give me the first nested array, still nested inside of an array:



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

var item = items.splice(0,1); // should slice first array out of items array

console.log(item); //should log [1,2], instead logs [[1,2]]


However it seems to return the needed array (first item), in another array. And I'm not able to get the full array unless I do item[0]. What in the world am I missing!?







javascript arrays multidimensional-array splice






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Sep 1 '15 at 2:55







user663031

















asked Sep 1 '15 at 2:17









Afs35mmAfs35mm

101211




101211








  • 1





    No need for splice. Just use items[0] instead.

    – PHPglue
    Sep 1 '15 at 2:22











  • Well I want to alter the original array and then chose a value based on an element of the spliced array.

    – Afs35mm
    Sep 1 '15 at 19:24














  • 1





    No need for splice. Just use items[0] instead.

    – PHPglue
    Sep 1 '15 at 2:22











  • Well I want to alter the original array and then chose a value based on an element of the spliced array.

    – Afs35mm
    Sep 1 '15 at 19:24








1




1





No need for splice. Just use items[0] instead.

– PHPglue
Sep 1 '15 at 2:22





No need for splice. Just use items[0] instead.

– PHPglue
Sep 1 '15 at 2:22













Well I want to alter the original array and then chose a value based on an element of the spliced array.

– Afs35mm
Sep 1 '15 at 19:24





Well I want to alter the original array and then chose a value based on an element of the spliced array.

– Afs35mm
Sep 1 '15 at 19:24












5 Answers
5






active

oldest

votes


















4














The MDN says Array.prototype.splice:




Returns



An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.




So, it won't return just the deleted element, it will be wrapped in an array.






share|improve this answer
























  • Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

    – Afs35mm
    Sep 1 '15 at 19:25



















0














splice() changes the contents of an array, and returns an array with the deleted elements.



In your case, your original array has elements that also happen to be arrays. That might be confusing you.






share|improve this answer































    0














    .splice() is returning correct array at item ; try selecting index 0 of returned array to return [1,2] ; to "flatten" item , try utilizing Array.prototype.concat() ; see How to flatten array in jQuery?






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

    var item = items.splice(0,1); // should slice first array out of items array
    // https://stackoverflow.com/a/7875193/
    var arr = .concat.apply(, item);

    console.log(item[0], arr);








    share|improve this answer

































      0














      You should reference the first array in items, and then splice it. Try working snippet below.






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

      var item = items[0].splice(0,2);

      console.log(item);








      share|improve this answer

































        -1














        To replace a two dimensional array you should use array[row][col] for example



        for(var row = 0; row < numbers.length; row++) {
        for(var col = 0; col < numbers[row].length; col++) {
        if (numbers[row][col] % 2 === 0) {
        numbers[row][col] = "even";
        } else {
        numbers[row][col] = "odd";
        }
        }

        }
        console.log(numbers);





        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%2f32322544%2fhow-to-splice-an-array-out-of-a-nested-array-javascript%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          4














          The MDN says Array.prototype.splice:




          Returns



          An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.




          So, it won't return just the deleted element, it will be wrapped in an array.






          share|improve this answer
























          • Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

            – Afs35mm
            Sep 1 '15 at 19:25
















          4














          The MDN says Array.prototype.splice:




          Returns



          An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.




          So, it won't return just the deleted element, it will be wrapped in an array.






          share|improve this answer
























          • Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

            – Afs35mm
            Sep 1 '15 at 19:25














          4












          4








          4







          The MDN says Array.prototype.splice:




          Returns



          An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.




          So, it won't return just the deleted element, it will be wrapped in an array.






          share|improve this answer













          The MDN says Array.prototype.splice:




          Returns



          An array containing the deleted elements. If only one element is removed, an array of one element is returned. If no elements are removed, an empty array is returned.




          So, it won't return just the deleted element, it will be wrapped in an array.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 1 '15 at 2:28









          MinusFourMinusFour

          8,74121828




          8,74121828













          • Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

            – Afs35mm
            Sep 1 '15 at 19:25



















          • Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

            – Afs35mm
            Sep 1 '15 at 19:25

















          Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

          – Afs35mm
          Sep 1 '15 at 19:25





          Kind of crazy I never knew that splice automatically places it inside a newly created array, I kind of just always assumed it worked that way because it was being taken from an already existing array. Thinking about it that makes sense because that's how Array.prototype.slice.call(arguments) works considering it's only an array-like object :)

          – Afs35mm
          Sep 1 '15 at 19:25













          0














          splice() changes the contents of an array, and returns an array with the deleted elements.



          In your case, your original array has elements that also happen to be arrays. That might be confusing you.






          share|improve this answer




























            0














            splice() changes the contents of an array, and returns an array with the deleted elements.



            In your case, your original array has elements that also happen to be arrays. That might be confusing you.






            share|improve this answer


























              0












              0








              0







              splice() changes the contents of an array, and returns an array with the deleted elements.



              In your case, your original array has elements that also happen to be arrays. That might be confusing you.






              share|improve this answer













              splice() changes the contents of an array, and returns an array with the deleted elements.



              In your case, your original array has elements that also happen to be arrays. That might be confusing you.







              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Sep 1 '15 at 2:24









              cybersamcybersam

              39.5k43151




              39.5k43151























                  0














                  .splice() is returning correct array at item ; try selecting index 0 of returned array to return [1,2] ; to "flatten" item , try utilizing Array.prototype.concat() ; see How to flatten array in jQuery?






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

                  var item = items.splice(0,1); // should slice first array out of items array
                  // https://stackoverflow.com/a/7875193/
                  var arr = .concat.apply(, item);

                  console.log(item[0], arr);








                  share|improve this answer






























                    0














                    .splice() is returning correct array at item ; try selecting index 0 of returned array to return [1,2] ; to "flatten" item , try utilizing Array.prototype.concat() ; see How to flatten array in jQuery?






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

                    var item = items.splice(0,1); // should slice first array out of items array
                    // https://stackoverflow.com/a/7875193/
                    var arr = .concat.apply(, item);

                    console.log(item[0], arr);








                    share|improve this answer




























                      0












                      0








                      0







                      .splice() is returning correct array at item ; try selecting index 0 of returned array to return [1,2] ; to "flatten" item , try utilizing Array.prototype.concat() ; see How to flatten array in jQuery?






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

                      var item = items.splice(0,1); // should slice first array out of items array
                      // https://stackoverflow.com/a/7875193/
                      var arr = .concat.apply(, item);

                      console.log(item[0], arr);








                      share|improve this answer















                      .splice() is returning correct array at item ; try selecting index 0 of returned array to return [1,2] ; to "flatten" item , try utilizing Array.prototype.concat() ; see How to flatten array in jQuery?






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

                      var item = items.splice(0,1); // should slice first array out of items array
                      // https://stackoverflow.com/a/7875193/
                      var arr = .concat.apply(, item);

                      console.log(item[0], arr);








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

                      var item = items.splice(0,1); // should slice first array out of items array
                      // https://stackoverflow.com/a/7875193/
                      var arr = .concat.apply(, item);

                      console.log(item[0], arr);





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

                      var item = items.splice(0,1); // should slice first array out of items array
                      // https://stackoverflow.com/a/7875193/
                      var arr = .concat.apply(, item);

                      console.log(item[0], arr);






                      share|improve this answer














                      share|improve this answer



                      share|improve this answer








                      edited May 23 '17 at 12:34









                      Community

                      11




                      11










                      answered Sep 1 '15 at 2:36









                      guest271314guest271314

                      79.2k643114




                      79.2k643114























                          0














                          You should reference the first array in items, and then splice it. Try working snippet below.






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

                          var item = items[0].splice(0,2);

                          console.log(item);








                          share|improve this answer






























                            0














                            You should reference the first array in items, and then splice it. Try working snippet below.






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

                            var item = items[0].splice(0,2);

                            console.log(item);








                            share|improve this answer




























                              0












                              0








                              0







                              You should reference the first array in items, and then splice it. Try working snippet below.






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

                              var item = items[0].splice(0,2);

                              console.log(item);








                              share|improve this answer















                              You should reference the first array in items, and then splice it. Try working snippet below.






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

                              var item = items[0].splice(0,2);

                              console.log(item);








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

                              var item = items[0].splice(0,2);

                              console.log(item);





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

                              var item = items[0].splice(0,2);

                              console.log(item);






                              share|improve this answer














                              share|improve this answer



                              share|improve this answer








                              edited Jan 20 at 0:14

























                              answered Jan 20 at 0:01









                              M'BoulasM'Boulas

                              45212




                              45212























                                  -1














                                  To replace a two dimensional array you should use array[row][col] for example



                                  for(var row = 0; row < numbers.length; row++) {
                                  for(var col = 0; col < numbers[row].length; col++) {
                                  if (numbers[row][col] % 2 === 0) {
                                  numbers[row][col] = "even";
                                  } else {
                                  numbers[row][col] = "odd";
                                  }
                                  }

                                  }
                                  console.log(numbers);





                                  share|improve this answer




























                                    -1














                                    To replace a two dimensional array you should use array[row][col] for example



                                    for(var row = 0; row < numbers.length; row++) {
                                    for(var col = 0; col < numbers[row].length; col++) {
                                    if (numbers[row][col] % 2 === 0) {
                                    numbers[row][col] = "even";
                                    } else {
                                    numbers[row][col] = "odd";
                                    }
                                    }

                                    }
                                    console.log(numbers);





                                    share|improve this answer


























                                      -1












                                      -1








                                      -1







                                      To replace a two dimensional array you should use array[row][col] for example



                                      for(var row = 0; row < numbers.length; row++) {
                                      for(var col = 0; col < numbers[row].length; col++) {
                                      if (numbers[row][col] % 2 === 0) {
                                      numbers[row][col] = "even";
                                      } else {
                                      numbers[row][col] = "odd";
                                      }
                                      }

                                      }
                                      console.log(numbers);





                                      share|improve this answer













                                      To replace a two dimensional array you should use array[row][col] for example



                                      for(var row = 0; row < numbers.length; row++) {
                                      for(var col = 0; col < numbers[row].length; col++) {
                                      if (numbers[row][col] % 2 === 0) {
                                      numbers[row][col] = "even";
                                      } else {
                                      numbers[row][col] = "odd";
                                      }
                                      }

                                      }
                                      console.log(numbers);






                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Oct 20 '17 at 21:25









                                      Alicia GuzmanAlicia Guzman

                                      2414




                                      2414






























                                          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%2f32322544%2fhow-to-splice-an-array-out-of-a-nested-array-javascript%23new-answer', 'question_page');
                                          }
                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          Popular posts from this blog

                                          Homophylophilia

                                          Updating UILabel text programmatically using a function

                                          Cloud Functions - OpenCV Videocapture Read method fails for larger files from cloud storage