...

View Full Version : detect an array from variable?



cortic
07-08-2012, 10:41 AM
Hi, i have this problem, e.g.


var arnameb = new Array()
var nameA = "arnameb"; //or it could contain "arnamec"


all i know is the var nameA could have the name of an array in the page, or might not, i do not have access to the array itself or know the name (its generated by perl), how do i test if the contents of nameA is equal to the name of an array on the page..?

thanks, not sure if this is even possible.

Philip M
07-08-2012, 11:10 AM
<script type="text/javascript">

var arnameb = new Array();
arnameb[0] = "Something";

try {
var nameA = arnameb; // or it could contain "arnamec"
nameA = eval(nameA);
alert (nameA[0]); // i.e. the value of the 0th element in the array if it exists
}
catch(err) {
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert (txt);
}
// optionally continue
finally {
alert ('This alert will show regardless of the outcome above');
}


</script>

Another approach which might suffice:-


alert (typeof arnameb);
alert (typeof arnamec);

Quizmaster: Where is the sea Of Tranquillity?
Contestant: Ibiza

cortic
07-08-2012, 11:24 AM
Philip M, great idea, worked like a charm, i never thought about try catch. thanks, saved me a headache today :)

oneguy
07-08-2012, 04:03 PM
You can use the built-in function Array.isArray to test if a variable is an array, however, it doesn't work in IE<9. For example:

alert(Array.isArray([])); //true
alert(Array.isArray(123)); //false
alert(Array.isArray({})); //false

oneguy
07-08-2012, 04:19 PM
<script type="text/javascript">

var arnameb = new Array();
arnameb[0] = "Something";

try {
var nameA = arnameb; // or it could contain "arnamec"
nameA = eval(nameA);
alert (nameA[0]); // i.e. the value of the 0th element in the array if it exists
}
catch(err) {
txt="There was an error on this page.\n\n";
txt+="Error description: " + err.message + "\n\n";
txt+="Click OK to continue.\n\n";
alert (txt);
}
// optionally continue
finally {
alert ('This alert will show regardless of the outcome above');
}


</script>
Firstly, it is incorrect, because nameA[0] will return undefined and not throw an exception if there is no property with index 0.
Secondly, it's a bad programming practice to test for an array by testing for existance of index properties - the array may be empty or non-array objects may contain index properties.


Another approach which might suffice:-


alert (typeof arnameb);
alert (typeof arnamec);

This is the best way if we don't need to distinguish arrays from other objects, because the typeof operator returns "object" for both arrays and other object which are not functions.

Philip M
07-08-2012, 04:38 PM
You can use the built-in function Array.isArray to test if a variable is an array, however, it doesn't work in IE<9. For example:

alert(Array.isArray([])); //true
alert(Array.isArray(123)); //false
alert(Array.isArray({})); //false

Yes, but there is a snag is the value to be tested is a variable:-


var myArray = [];

var x = eval("myArray");
alert ("myArray " + Array.isArray(x)); // true
var x = eval ("notAnArray");
alert ("notAnArray " + Array.isArray(x)); // error



alert (nameA[0]); // i.e. the value of the 0th element in the array if it exists


As you say, returns undefined if the element of the named array does not exist. But if the array does not exist at all then the error is triggered.


var arnameb = new Array();
var arnamec = "Philip";

try {
var nameA = arnameb; //or it could contain "arnamec"
nameA = eval(nameA);
alert (nameA.length);
alert ("The variable is an array");
alert (nameA[0]); // alert the 0th element of the array, or undefined if that does not exist

}

oneguy
07-08-2012, 05:16 PM
Yes, but there is a snag is the value to be tested is a variable:-


var myArray = [];

var x = eval("myArray");
alert ("myArray " + Array.isArray(x)); // true
var x = eval ("notAnArray");
alert ("notAnArray " + Array.isArray(x)); // error


Array.isArray works on all values and never throws an error. In this example it is the second last line (with eval) which throws an error, not the last one.





var arnameb = new Array();
var arnamec = "Philip"

try {
var nameA = arnameb; //or it could contain "arnamec"
nameA = eval(nameA);
alert (nameA.length);
alert (nameA + " is an array);
}
This isn't good either. Non-array objects may contain the length property as well.
Does the OP need to test for existance of the variable?

Philip M
07-08-2012, 05:37 PM
Array.isArray works on all values and never throws an error. In this example it is the second last line (with eval) which throws an error, not the last one.


So how would you test for the existence of an array whose name is a variable? Which is what I understand the OP to require.
eval() applied to something which does not exist results in an error, as you say.

The code which I posted works just fine, and I do not really see what is the point you are making. If arnameb is a string variable (not an array) then the error message is triggered.

I do accept that if anameb exists as a function then it is wrongly identified as an array. How likely that is to occur is up to the OP to judge.

oneguy
07-09-2012, 01:25 AM
If arnameb is a string variable (not an array) then the error message is triggered.
If you mean checking for the length property then it isn't true. Every String object also has the length property. If nameA is a string, nameA.length will return the length of the string (the string value is converted to a String object before applying the dot operator).

Philip M
07-09-2012, 09:07 AM
Nonsense. I don't think you have even tried the code.

Add:

var arnamec= "Micky Mouse"; // a string

and

var nameA = arnamec;

Then the error is triggered by nameA = eval(nameA);


You have still not suggested how you would test for the existence of an array whose name is a variable.

Richter
07-09-2012, 10:04 AM
I agree with oneguy, we could simply check it's exit and type in one line.



var Test = [1, 2, 3], Test2 = "Success";

var Result = [
(typeof Test !== "undefined") ? Array.isArray(Test) : false,
(typeof Test2 !== "undefined") ? Array.isArray(Test2) : false,
(typeof Test3 !== "undefined") ? Array.isArray(Test3) : false
];

var Names = ["Test", "Test2", "Test3"];
var Result2 = [];

for (var Item in Names) {
var Tmp = Names[Item];
Result2.push(
new Function("return (typeof " + Tmp + " !== 'undefined') ? Array.isArray(" + Tmp + ") : false;")()
);
}

Philip M
07-09-2012, 10:10 AM
I agree with oneguy, we could simply check it's exit and type in one line.


Yes, but is does not work in IE<9. :( There are still very many IE browser versions <9 still about.

And one more time, how do you test for an array whose name is a variable?

Richter
07-09-2012, 10:27 AM
I add test with name of variant but still not test with IE9 or lower (since I use windows 8 I can't test it :( ), please tell me if it not work on IE9 or lower.

Philip M
07-09-2012, 10:29 AM
I add test with name of variant but still not test with IE9 or lower (since I use windows 8 I can't test it :( ), please tell me if it not work on IE9 or lower.

No idea. I use IE9.

oneguy
07-09-2012, 04:52 PM
Nonsense. I don't think you have even tried the code.
Add:
var arnamec= "Micky Mouse"; // a string
and
var nameA = arnamec;
Then the error is triggered by nameA = eval(nameA);
You have still not suggested how you would test for the existence of an array whose name is a variable.
This is true, but you might have made an error in your code: the OP wrote

var arnameb = new Array()
var nameA = "arnameb"; //or it could contain "arnamec"
but you wrote the 2nd line as


var nameA = arnameb;

Richter has suggested a solution, but there is a problem: new Function always creates a function with the global scope but the given variable may be in a local scope. Therefore it's better to use eval. Here is my solution:


//this is a workaround for IE<9
if (!Array.isArray)
Array.isArray=function (arg) {
return Object.prototype.toString.call(arg)=="[object Array]";
};

var arnameb = new Array()
var nameA = "arnameb"; //or it could contain "arnamec"
//this code tests if the variable whose name is contained in nameA exists and is an array
alert(eval('typeof '+nameA+'!="undefined"&&Array.isArray('+nameA+')'));

The typeof operator returns "undefined" for a non-existent variable.

Richter
07-09-2012, 05:36 PM
Thanks oneguy but I think eval is evil (http://javascriptweblog.wordpress.com/2010/04/19/how-evil-is-eval/) , so I will stick myself with function constructor :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum