www.webdeveloper.com
Results 1 to 3 of 3

Thread: how do I convert a 1D array to 2D array in javascript?

Hybrid View

  1. #1
    Join Date
    Jan 2014
    Posts
    7

    how do I convert a 1D array to 2D array in javascript?

    I hav an array m1=[1,2,3,4,5,6,7,9]
    I want to convert it to m1=[[1,2,3],[4,5,6],[7,8,9]]
    what can be the simplest and fastest way to do so?

  2. #2
    Join Date
    Dec 2012
    Posts
    97
    You could just keep splicing the array until all the items are in a secondary, now multidimensional, array

    HTML Code:
    <script>
    var arr = [1,2,3,4,5,6,7,8,9];
    var nArr = [];
    while(arr.length > 0) {
    	nArr.push(arr.splice(0,3));
    }
    </script>

  3. #3
    Join Date
    Jan 2014
    Posts
    7

    Talking

    Quote Originally Posted by iBeZi View Post
    You could just keep splicing the array until all the items are in a secondary, now multidimensional, array

    HTML Code:
    <script>
    var arr = [1,2,3,4,5,6,7,8,9];
    var nArr = [];
    while(arr.length > 0) {
    	nArr.push(arr.splice(0,3));
    }
    </script>
    Thats cool!.. thanks

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles