1

I have to get some records based on weekly basis for the last weeks, and have to add values from records of one week to an array. So, I declared 6 arrays to store 6 weeks records. My code is:

var w_0 = [];var w_1 = [];var w_2 = [];var w_3 = [];var w_4 = [];var w_5 = [];
var myTotal = 0;
var arr_name = "";

for(var j=0;j<=5;j++)
{

    var start_date="";
    var end_date="";

    //code to fetch the records added between start_date,end_date
    //there may be more that one record

    var count = getRecordCount(); //My function
    //following loop is to fetch value from a record
    for(var i=0;i<count;i++)    
    {
        var val1 = getRecordByIndex(i).getValue("rem_val"); //getRecordByIndex() and getValue() are our pre-defined functions. 

        //here I want to push the values into the array w_0

        arr_name = "w_"+j;
        [arr_name].push(val1); //this is not working
        alert([arr_name]); //showing 'w_0'

    } 

    //and here I want to sum all the array elements when i reaches its maximum
    for(var a=0;a<[arr_name].length; a++){
        myTotal += parseInt([arr_name][a]);  
    }
    alert("Total value of week"+j+"="+parseInt(myTotal)); 
}

How can I add values of inner loop to the array based on outer loop?

4
  • You can't access variables by name. Why don't you just use a 2-dimensional array? Commented Jan 23, 2014 at 12:12
  • [arr_name] isn't a reference to the array whose name is in arr_name. It's an array containing the string in arr_name. Commented Jan 23, 2014 at 12:15
  • ["string"] creates an array which contains "string". If we change your code to instead use var a = ["string"]; a.push("val"); we'd end up with ["string", "val"]. This is what your code currently does; it creates an array containing a string (which is equal to the name of whichever of your array variables) which isn't assigned to any variable itself, then pushes val1 into it. Commented Jan 23, 2014 at 12:16
  • will this work then. var all_arrays = {w_0:[],w_1:[],w_2:[],w_3:[],w_4:[],w_5:[]}; arr_name = "w_"+j;all_arrays[arr_name].push(val1); Commented Jan 23, 2014 at 12:22

2 Answers 2

1

Any time you find yourself creating variables with sequentially numbered names, you should probably be using an array instead.

var w = [[], [], [], [], []];

Then, wherever you tried to use [arr_name] to refer to a particular w_j variable, you should use w[j].

for(var j=0;j<=w.length;j++)
{

    var cur_w = w[j];
    var start_date="";
    var end_date="";

    //code to fetch the records added between start_date,end_date
    //there may be more that one record

    var count = getRecordCount(); //My function
    //following loop is to fetch value from a record
    for(var i=0;i<count;i++)    
    {
        var val1 = getRecordByIndex(i).getValue("rem_val"); //getRecordByIndex() and getValue() are our pre-defined functions. 

        cur_w.push(val1);
        alert(cur_w);

    } 

    //and here I want to sum all the array elements when i reaches its maximum
    for(var a=0;a<cur_w.length; a++){
        myTotal += parseInt(cur_w[a]);  
    }
    alert("Total value of week"+j+"="+parseInt(myTotal)); 
}
Sign up to request clarification or add additional context in comments.

1 Comment

You need to set myTotal to 0 before the totaling loop.
0

If you want to dynamically manipulate global variables you can use window prefix:

arr_name = "w_"+j;
window[arr_name].push(val1); // This should work

2 Comments

You can only do that with global variables, not local variables.
For local you can create object that contains all of the arrays and use that instead of the window prefix.. something like var store = {}; store.w_1 = []; etc..

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.