0

This an array

 ARRAY = ["A","B","C","D","E"];
 var mlength = ARRAY.length;

Depending on mlength the length of DATES must be declared. Here the length is 5. Hence the array DATES must be var DATES = [[], [], [], [], []]; similarly if array ARRAY length is 3 then var DATES = [[], [], []]

This is static declaration. How do I declare it based upon the array ARRAY length?

7
  • 1
    How are you declaring these variables? I feel you're trying to solve this the wrong way... Commented Mar 23, 2015 at 16:03
  • Why not just store your values in one nested array? Commented Mar 23, 2015 at 16:05
  • I have edited my code. I just want to declare an empty array. Later I will push the values into it. That is a different coding. Only the size of array depends on the number of variables Commented Mar 23, 2015 at 16:09
  • Use an object , really better way to do this Commented Mar 23, 2015 at 16:14
  • @rjirji but the size doesn't matter at all, if you are only pushing values onto it. Commented Mar 23, 2015 at 16:14

2 Answers 2

3

Well, this is easy:

var DATES=[];for (var i=0;i<mlength;DATES.push([]),i++);

Just push an empty array to DATES mlength number of times.

Sign up to request clarification or add additional context in comments.

4 Comments

Forget everything. Please see the question again. I have changed it completely
"Please see the question again. I have changed it completely" This is considered bad form on SO, by the way. If you are tempted to change your question that much, create a new question, and delete this one if necessary.
@theonlygusti - I got it thanq :)
@theonlygusti - Can you do me one more favour? How do I declare MARKETS[i].push(parseFloat(findDataForMonth(mname+i, month)) || null); in the same manner where i=mlength here MARKETS[i] and mname+i are to be replaced by i in for loop
0

Alternative solution using Array.prototype.fill()

ARRAY = ["A","B","C","D","E"];
var mlength = ARRAY.length,
    DATES = [];

    DATES.length = mlength;
    DATES.fill([]);
    //DATES is now -> [ [], [], [], [], [], ]

4 Comments

It should be noted this depends on the array having a fill method, which is an ES6 feature not implemented in all browsers (specifically, not in IE). See this compatibility table for more information.
Wouldn't this create the sub-array only once, and assign its reference to every slot of DATES?
@MikeMcCaughan - its not working. I will show you the complete code. see this link its not working. I will show you my complete code. see this link jsfiddle.net/xbk57jdo/5 in line 111
@rjirji, I'm not sure why you're pinging me about it. I wrote that comment to tell you (and everyone else) that the code most likely would not work, so it's not surprising to me that it does, in fact, not work.

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.