1

I have a single dimension array of objects:

var myArray = [ { record  : "value" ,
                  recod2  : "value" ,
                  record3 : "value"
                } ,
                { record  : "value" ,
                  record2 : "value"
                  record3 : "value"
                } ,
                .
                .
                .
              ] ;

From this, I want to construct a jagged 2-dimensional array (array of arrays) like so:

var result = [ [ {} , {} , {} , {} ] ,
               [ {} , {} , {} , {} ] ,
               [ {} , {} , {} , {} ] ,
               .
               .
               .
             ] ;

For example, If my first array contains 8 objects, The result array should look like this:

var result = [ [ obj1 , obj2 , obj3 , obj4 ] ,
               [ obj5 , obj6 , obj7 , obj8 ] ,
             ] ;

How can I achieve this with a for loop ?

Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
icdev
  • 11
  • 1
  • 2

3 Answers3

2
var result = [], data = /* above object */;
for (var i = 0; i < data.length; i++){
  if (i % 4 == 0) result.push([]);
  result[Math.floor(i / 4)].push(data[i]);
}

Should do the trick. Every 4th object we begin a new (nested) array and add the elements to it.

Output (assuming 8 elements):

[
  [
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"}
  ],
  [
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"},
    {"record": "value",
      "record2": "value",
      "record3": "value"}
  ]
]
Brad Christie
  • 100,477
  • 16
  • 156
  • 200
1

Have you actually tried anything? Some code showing your tries would be appreciated. However...

You can use the slice() method offered by javascript's array object:

func transform2d( srcArray , modulus )
{
    var result = undefined ;
    if ( modulus > 0 )
    {
      result = [] ;
      for ( var i = 0 ; i < srcArray.length ; i+= modulus )
      {
        result.push( srcArray.slice( i , i+modulus ) ) ;
      }
    }
    return result ;
}

You should note that if you don't mind destroying the source array in the process, you could use the splice() method as well:

func transform2d( srcArray , modulus )
{
    var result = undefined ;
    if ( modulus > 0 )
    {
      result = [] ;
      while ( srcArray.length > 0 )
      {
        result.push( srcArray.splice( 0 , modulus ) ) ;
      }
    }
    return result ;
}

Due to the side effects, I'd consider the splice() approach to be...suboptimal.

Apparently, great minds think alike. See this answer as well: https://stackoverflow.com/a/8495740/467473

Community
  • 1
  • 1
Nicholas Carey
  • 71,308
  • 16
  • 93
  • 135
0

Overshoot using a simple loop, then work your way backwards removing excess undefined.

var myArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
var result = [], i;
for (i = 0; i < myArray.length; i += 4) { // adding 4 each time
    result.push([myArray[i], myArray[i + 1], myArray[i + 2], myArray[i + 3]]);
}
// then if you have length !== 0 mod 4, work backwards
while ((i = result.length - 1) * 4 + result[i].length > myArray.length)
     result[i].pop(); // removing items from the end
// now you are left with
result; // [[0, 1, 2, 3], [4, 5, 6, 7], [8, 9]]
Paul S.
  • 64,864
  • 9
  • 122
  • 138