0

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. 
    }
}
1
  • This seems more like a generic javascript question, not specific to jQuery... Commented Sep 8, 2015 at 13:23

2 Answers 2

1

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();
    }
 });
}
Sign up to request clarification or add additional context in comments.

Comments

0

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.