0

I have an example data set. I want to push this data into an array with a subarray for every 16 pieces of data. I got started, but am stuck. Any ideas?

[ [16 names], [16 names ], [16 names] ]; // This is the output I want.

var data = ["Michael", "Marc", "John", "David", "Nick", "Mom", "Al", "Marlana", "Max", 
            "Scooter", "Pat", "George", "Lynne", "Tatyana", "Kim", "Kerry", "Enza", "Matt",
            "Liz", "Linda", "Ryan",
            "Ed", "Frank", "Christine", "Bill", "Jack", "Jill", "Joe", "Harry", "Red",
            "Fred", "Iggy", "Brian", "Bob", "Paul", "Gil", "Damian", "Kip", "Phil", "Curtis", "Aly",
           "Richard", "Robin", "Grant", "Ian", "Raptor", "T-Rex", "Stegosaurux", "Triceratops", "Compy"]


var sixteengroup = [];

for (var i = 0; i < data.length; i++){
  if (i % 16 == 0) sixteengroup.push([i])
}


console.log(sixteengroup);
mplungjan
  • 169,008
  • 28
  • 173
  • 236

5 Answers5

3

Splice (destructive) or slice sounds like a good idea

var data = ["Michael", "Marc", "John", "David", "Nick", "Mom", "Al", "Marlana", "Max","Scooter", "Pat", "George", "Lynne", "Tatyana", "Kim", "Kerry", "Enza", "Matt","Liz", "Linda", "Ryan", "Ed", "Frank", "Christine", "Bill", "Jack", "Jill", "Joe", "Harry", "Red", "Fred", "Iggy", "Brian", "Bob", "Paul", "Gil", "Damian", "Kip", "Phil", "Curtis", "Aly", "Richard", "Robin", "Grant", "Ian", "Raptor", "T-Rex", "Stegosaurux", "Triceratops", "Compy"]

function chunk(source,size) { // non-destructive
  var arrofarr=[], cnt=0;
  while (cnt < source.length) { 
    arrofarr.push(source.slice(cnt,cnt+size)); 
    cnt+= arrofarr[arrofarr.length-1].length;
  }
  return arrofarr;
}  
console.log(chunk(data,16));

// ----------------------------------

function chunkie(source,size) { // destructive
  var arrofarr=[];
  while (source.length) { arrofarr.push(source.splice(0,size)); }
  return arrofarr;
}
console.log(chunkie(data,16));
mplungjan
  • 169,008
  • 28
  • 173
  • 236
1

Try this:

var data = ["Michael", "Marc", "John", "David", "Nick", "Mom", "Al", "Marlana", "Max", 
        "Scooter", "Pat", "George", "Lynne", "Tatyana", "Kim", "Kerry", "Enza", "Matt",
        "Liz", "Linda", "Ryan",
        "Ed", "Frank", "Christine", "Bill", "Jack", "Jill", "Joe", "Harry", "Red",
        "Fred", "Iggy", "Brian", "Bob", "Paul", "Gil", "Damian", "Kip", "Phil", "Curtis", "Aly",
       "Richard", "Robin", "Grant", "Ian", "Raptor", "T-Rex", "Stegosaurux", "Triceratops", "Compy"]


var sixteengroup = [];

var x = -1;
for (var i = 0; i < data.length; i++){

    if (i % 16 == 0) {
        sixteengroup[++x] = [data[i]]
        continue;
    } 

    sixteengroup[x].push(data[i])

}

console.log(sixteengroup);

Fiddle here: https://jsfiddle.net/n9ro2vwr/1/

Arnaldo Capo
  • 178
  • 1
  • 10
  • Please expand on why so that in the future I avoid inefficiency. Maybe the world a better place. – Arnaldo Capo Apr 26 '15 at 20:46
  • You add them one by one. Then you complain my splice/slice suggestion was accepted? I abandoned the loop I wrote about 5 minutes before you wrote yours – mplungjan Apr 26 '15 at 21:04
0

Try the following:

function split(a, n) {
    var len = a.length,out = [], i = 0;
    while (i < len) {
        var size = Math.ceil((len - i) / n--);
        out.push(a.slice(i, i += size));
    }
    return out;
}
var data = ["Michael", "Marc", "John", "David", "Nick", "Mom", "Al", "Marlana", "Max", 
            "Scooter", "Pat", "George", "Lynne", "Tatyana", "Kim", "Kerry", "Enza", "Matt",
            "Liz", "Linda", "Ryan",
            "Ed", "Frank", "Christine", "Bill", "Jack", "Jill", "Joe", "Harry", "Red",
            "Fred", "Iggy", "Brian", "Bob", "Paul", "Gil", "Damian", "Kip", "Phil", "Curtis", "Aly",
           "Richard", "Robin", "Grant", "Ian", "Raptor", "T-Rex", "Stegosaurux", "Triceratops", "Compy"]

sixteengroup = split(data, data.length/16);
document.write(JSON.stringify(sixteengroup));
A.J. Uppal
  • 19,117
  • 6
  • 45
  • 76
0

i use this function to group by any number

  groupBy = function (array,howmany)
    {
    var newarray =[];
    index=0;
    for(a=0;a<array.length;a++){
       if(a%howmany==0){
          if(a!=0)
           index++;               
          newarray[index]=[];
          newarray[index].push(array[a]);

      }else{
         newarray[index].push(array[a]);

      }
   }
  return newarray;
}

 items=groupBy(data,16);
A.B
  • 20,110
  • 3
  • 37
  • 71
0

maybe make a function with 2 arguments to have the array and the second argument to limit the amount of items per second denominational array like so?

function data(arr, size) {
  var array = [];
    while(arr.length > 0){
         array.push(arr.splice(0,size));   
    }
  return console.log(array);
}

data(["Michael", "Marc", "John", "David", "Nick", "Mom", "Al", "Marlana", "Max", 
            "Scooter", "Pat", "George", "Lynne", "Tatyana", "Kim", "Kerry", "Enza", "Matt",
            "Liz", "Linda", "Ryan",
            "Ed", "Frank", "Christine", "Bill", "Jack", "Jill", "Joe", "Harry", "Red",
            "Fred", "Iggy", "Brian", "Bob", "Paul", "Gil", "Damian", "Kip", "Phil", "Curtis", "Aly",
           "Richard", "Robin", "Grant", "Ian", "Raptor", "T-Rex", "Stegosaurux", "Triceratops", "Compy"], 16)

https://jsfiddle.net/ToreanJoel/dq7t7snx/

You can play around and see if its to your liking.

TrojanMorse
  • 642
  • 1
  • 8
  • 16