Nithin
Nithin

Reputation: 255

How to add dynamically growing array into another dynamic array

In my project i have arrays for date1, date2 and so on upto one month dates as shown below

var day1= [4,5,6,11];
var day2= [7,8,9,12];
var day3= [10,11,12,14];...

var day30= [1,2, 3, 4];

In the above each array exact size we dont know that may increase or decrease and i need to pass these values to the one more set of arrays like

var data1= [day1[0], day2[0], day3[0]];
var data2= [day1[1], day2[1], day3[1]];
var data3= [day1[2], day2[2], day3[2]];...

var data30= [day1[30], day2[30], day3[30]];

As per the day array size data array size will increase.

How to solve this issue any help..?

Upvotes: 0

Views: 96

Answers (2)

RobG
RobG

Reputation: 147363

Consider using an object instead of variables:

var days = {
  day1: [4,5,6,11],
  day2: [7,8,9,12],
  ... 
  day30: [1,2, 3, 4]
};

Then you can do something like:

var data30 = [];
var i = 0;
while ( days.hasOwnProperty('day' + ++i) ){
  data30.push(days['day' + i]);
}

Though given the sample data, data30 won't have any members because the first array has no member at index 30.

Upvotes: 1

Chris Gessler
Chris Gessler

Reputation: 23113

Check out JavaScript's push method.

http://www.w3schools.com/jsref/jsref_push.asp

Upvotes: 0

Related Questions