Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    New Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    detect an array from variable?

    Hi, i have this problem, e.g.
    Code:
    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.
    corrie track

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Code:
    <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:-

    Code:
    alert (typeof arnameb);
    alert (typeof arnamec);
    Quizmaster: Where is the sea Of Tranquillity?
    Contestant: Ibiza
    Last edited by Philip M; 07-08-2012 at 11:06 AM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #3
    New Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    55
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Philip M, great idea, worked like a charm, i never thought about try catch. thanks, saved me a headache today
    corrie track

  • #4
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    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:
    Code:
    alert(Array.isArray([])); //true
    alert(Array.isArray(123)); //false
    alert(Array.isArray({})); //false
    Last edited by oneguy; 07-08-2012 at 03:05 PM.

  • #5
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by Philip M View Post
    Code:
    <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:-

    Code:
    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.
    Last edited by oneguy; 07-08-2012 at 03:25 PM.

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by oneguy View Post
    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:
    Code:
    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:-

    Code:
    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.

    Code:
    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
    
    }
    Last edited by Philip M; 07-08-2012 at 04:29 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #7
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by Philip M View Post
    Yes, but there is a snag is the value to be tested is a variable:-

    Code:
    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.


    Code:
    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?
    Last edited by oneguy; 07-08-2012 at 04:19 PM.

  • #8
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by oneguy View Post
    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.
    Last edited by Philip M; 07-08-2012 at 04:42 PM.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #9
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by Philip M View Post
    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).
    Last edited by oneguy; 07-09-2012 at 12:40 AM.

  • #10
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #11
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    I agree with oneguy, we could simply check it's exit and type in one line.

    Code:
    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;")()
    ); 
    }
    Last edited by Richter; 07-09-2012 at 09:24 AM. Reason: Add how it work with string array that contain name of variant.

  • #12
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Richter View Post
    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?

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #13
    New Coder
    Join Date
    Jun 2012
    Posts
    80
    Thanks
    0
    Thanked 18 Times in 18 Posts
    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.

  • #14
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,919
    Thanks
    203
    Thanked 2,531 Times in 2,509 Posts
    Quote Originally Posted by Richter View Post
    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.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #15
    New Coder
    Join Date
    Jul 2012
    Location
    Ukraine
    Posts
    71
    Thanks
    1
    Thanked 18 Times in 17 Posts
    Quote Originally Posted by Philip M View Post
    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
    Code:
    var arnameb = new Array()
    var nameA = "arnameb"; //or it could contain "arnamec"
    but you wrote the 2nd line as
    Code:
    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:
    Code:
    //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.
    Last edited by oneguy; 07-09-2012 at 04:09 PM.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •