0

1.I want to push id[i] into global array.After I pushed then it will be id.length numbers items in each array,but it will be error and says y is not a function.how to solve it?

2.the sendrequest in the bottom will send a xmlhttprequest to server,but I don't understand why it will run first and then the function(parResponse) will be fire after tmpReadRequest.sendReadRequest(); has down.

Thanks a lot

    var tes0=new Array();
    var tes1=new Array();
    var tes2=new Array();
    var tes4=new Array();
    var tes5=new Array();
    function ReadValuePath(id, Variable,id_2) {
        var tmpReadCB = function(parResponse)
        {
            for (var tmpIndex = 0; tmpIndex < parResponse.length; tmpIndex++)
            {
                var tmpItemValue = parResponse[tmpIndex];//console.log(tmpItemValue);
                var tmpValue = (tmpItemValue.mItemValue) ? tmpItemValue.mItemValue : tmpItemValue.mItemResultId;
                if(document.getElementById(id[tmpIndex]) != null && document.getElementById(id_2[tmpIndex]).value != 0)
                {   
                    document.getElementById(id[tmpIndex]).value = parseFloat(tmpValue).toFixed(2);  
                }
            }
            return true;
        }
        var tmpReadRequest = new OPCReadRequest("DE", tmpReadCB);
           for(var z=0;z<5;z++ ){
           for(var i = 0; i < id.length; i++)
           var y="tes"+z;
           y.push(id[i]);
            tmpReadRequest.addItem("ab", Variable[i]);
        }
}
    tmpReadRequest.sendReadRequest();
}
4
  • y is not an array therefore it does not have a push method hence your exception. Unless it is a global variable in which case is is being overwritten by var y="tes"+z. Commented Nov 19, 2015 at 13:29
  • how to access the global array var tes0 to 5?? Commented Nov 19, 2015 at 13:30
  • You can use either the window object and a string literal or change your structure slightly so your array are in a object then use the string literal of that. So the first option would be window[y].push(id[i]); Commented Nov 19, 2015 at 13:38
  • but my purpose is to loop through the tes0~5 using the z Commented Nov 19, 2015 at 13:43

2 Answers 2

1

"A variable declared outside a function, becomes GLOBAL.

A global variable has global scope: All scripts and functions on a web page can access it. " @ Source

Y is not an array so .push() won't work on it. @ Source

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

Comments

0

To access the global scope through a string literal like you are trying you can use the window object which is the current global scope.

So in your case it would be window[y].push(id[i]);

Another option would be to change you scructure slightly as personally i don't like accessing the window directly.

so you could define your arrays like

var arrays = {
    tes0: [],
    tes2: [],
    tes3: [],
    tes4: [],
    tes5: [],
}

and access them like:

arrays[y].push(id[i])

EDIT according to comments

So you want to access global variable in a loop. You are half way there. What your doing is building a string y which contains the property name then using that in the square brackets to access that property.

So with the window option that would be:

for(var z=0;z<5;z++ ){
    var y="tes"+z;
    for(var i = 0; i < id.length; i++)
        window[y].push(id[i]);
    }
}

or with the second object option

/*
    Because arrays is an object we can use Object.keys
    This will return an array of the keys in our object which we can loop over to access each one
*/
Object.keys(arrays).forEach(function(key) {
    for(var i = 0; i < id.length; i++)
        arrays[key].push(id[i]);
    }
});

Hope that helps explain

4 Comments

but my purpose is to loop through the tes0~5 using the z
Thanks a lot.I got it ,do you have any idea about the second?
I cannot find any docs on OPCReadRequest however it seems like most async API's. You pass it your function tmpReadCB which it lloks like it will call once its task is complete. You start the task with tmpReadRequest.sendReadRequest(); then it will call tmpReadCB once complete with the result. Its a callback.
the result is not as I thought,it will push each id to each array,then all the array is look like the same,but what I want is to pust 1~5 to array1 push 6~10 to array2 like that,do you know how to do?

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.