user1629977
user1629977

Reputation: 437

how to create array dynamically using jQuery

I have below static array. But it is in static format. I am trying to create same things dynamically.

[
  ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
  ['2012', 10, 11, 12, 13, 15, 16],
  ['2013', 10, 11, 12, 13, 15, 16],
  ['2014', 10, 11, 12, 13, 15, 16],
  ['2015', 10, 11, 12, 13, 15, 16],
  ['2016', 10, 11, 12, 13, 15, 16]
];

this.getDynamically = function(data) {
    for(var i=0 ; i< data.count ;i++) {
        // here this data will be create one by one row. 
    }
}

Upvotes: 0

Views: 52

Answers (2)

Bhushan Kawadkar
Bhushan Kawadkar

Reputation: 28513

Try this : You can iterate your data and create array for first 7 elements and put it in another array and reset counter to zero. Again repeat same process for next 7 elements.

this.getDynamically = function(data) {
 var a = new Array();
 var b = new Array();
 var count = 0;
 jQuery.each(data, function(i,v){
    b.push(v);
    count++;
    if(count == 7)
    {
      count = 0;
      a.push(b);
      b = new Array();
    }
 });
}

Upvotes: 1

rrk
rrk

Reputation: 15846

Use Array.prototype.push() function in the loop.

var a = [
  ['', 'Kia', 'Nissan', 'Toyota', 'Honda', 'Mazda', 'Ford'],
  ['2012', 10, 11, 12, 13, 15, 16],
  ['2013', 10, 11, 12, 13, 15, 16],
  ['2014', 10, 11, 12, 13, 15, 16],
  ['2015', 10, 11, 12, 13, 15, 16],
  ['2016', 10, 11, 12, 13, 15, 16]
];

a.push( ['2017', 10, 11, 12, 13, 15, 16])
console.log(a);

Upvotes: 0

Related Questions