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..?
Jigar Joshi
241k42 gold badges409 silver badges446 bronze badges
asked May 15, 2012 at 10:33
Nithin
2551 gold badge6 silver badges16 bronze badges
2 Answers 2
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.
Sign up to request clarification or add additional context in comments.
Comments
Check out JavaScript's push method.
http://www.w3schools.com/jsref/jsref_push.asp
answered May 15, 2012 at 10:39
Chris Gessler
23.2k8 gold badges60 silver badges86 bronze badges
2 Comments
RobG
Please don't reference w3schools, reference the appropriate specification (W3C for DOM or ECMAScript for pure language items) and MSDN or MDN if examples seem useful.
Yoshi
ref: w3fools.com for why w3schools might not be the best reference to link to.
lang-js
day1..day30you could use arrays. Array of arrays