Maybe better off is to make this array an "array of arrays"
(JS's approach to a multi-dim array). Then, each element
would have a "name" [0] and "value" [1] pair.
PHP Code:
<script type="text/javascript">
function testIt() {
var str="";
var myArray = new Array();
var colorArray = new Array("Green","Red","Blue");
for (var i=0; i<tempArray.length; i++) {
var temp = new Array();
temp[0] = i+10;
temp[1] = colorArray[i];
myArray[i] = tempj;
}
Working web site is not the one that looks the same in a few graphical browsers, but the one that adequately delivers its content to any device accessing it.
I just want to be able to access every element in the passed array through the arguments array. You might ask why don't you use the passed array directly instead of the arguments array. The reason is, that I use the same function in another context where the function ist used this way:
Test(23, 345, 2 ,3);
and then I use the arguments array to access the passed variables.
I would have to check if the first argument is an array (with typeof) and then fork the whole thing and write a special part only for the array. I would be easier if I just could pass every value of the array to the function such as
Test(arrTest[0], arrTest[1], arrTest[2],..) but I don't know the length of arrTest.
Working web site is not the one that looks the same in a few graphical browsers, but the one that adequately delivers its content to any device accessing it.
Yep, you're right, why not doing it the easy way. But I'm curious if there is not a way to make it work (that's why i used the toString()) with somethin like:
What is the structure of this function right now. It apparently
accepts string/number variables and not of type array().
How many variable arguments are there right now? It seems
the best course is to re-write it so that it only accepts an
array. Maybe this is what vladdy has already mentioned.
I didn't mean to confuse you. I will do what Vladdy's suggested, e.g. only pass arrays to the function and then instead of working with the arguments array I work with the passed array. But if you want to know what I was talking about:
I have a function that receives a unknown number of arguments which are normally just numbers (would work with strings as well) for example
Test(2,10,56,4);
inside the function I access the passed numbers:
Test() {
while (i < arguments.length) {
alert(arguments[i]);
i++;
}
but if I pass an array
Test(arrTest);
then arguments.length = 1 and arguments[0] holds the passed array.
But what I wanted is that the array doesn't get passed as an array but as single comma separated values so as the while loop would loop through all elements of the array.
That's why I tried to do
Test(arrTest.toString());
Now with Vladdy's suggestion I replace the arguments loop with the passed array and pass the single numbers also as an array:
e.g.
I have a function that receives a unknown number of arguments
I guess this phrase is whats throwing me. Correct me, if you
like, so I understand what you mean. It sounds as if you
have an unknown amount of values (delimited by commas)
contained in "one" variable argument.
So the original function structure appeared something like??
function test(val) {
}
OK - I'm stupid. Not familiar with that type of structure. Of
course, this appears as if JS is "implicitly" handling
the passed values as an Array, contained in the "arguments"
object.
So to clear my mind up, you are now going to "explicitly"
prepare the function to accept an Array() argument??
you are only sending one argument if you send an array. The array however has the length of what you need. You could do a simple .split:
PHP Code:
function split(val) {
var myVals = val.split(',')
}
That would take the array you sent as a string and create it back into an array.Then of course myVals.length would be what you want, not arguments.length
My problem is, that I do not always call the function Test() in the way:
Test(2,10,56,4);
but also in the way:
var arrTest = new Array(2,10,56,4);
Test(arrTest);
and then arrTest is seen only as 1 argument in the Test() function (arguments.length = 1) , but I wanted the while loop to output all the elements in the array.
As Vladdy pointed out instead of trying the arguments object to accept each element of the array as an argument I should just rewrite the Test() function:
PHP Code:
function Test(arrTest) {
for (var i = 0; i < arrTest.length; i++)
alert(arrTest[i]);
}
This solution doesn' really make me happy, because I use the Test() function severall times and I don't want to create every time a new array object that I only use once. So I think about using typeof to check if the first argument is an array or not and then...
Bookmarks