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.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts

    Referencing object array dynamically

    Hi all,

    I'm wondering if what I'm trying to do here is even possible but I'd appreciate your thoughts on it.

    Code:
    <script type="text/javascript">
    
    	function test() {
    		var arrays = {
    			one: ['1', '2', '3'],
    			two: ['a', 'b', 'c']
    		}
    		
    		var foo = 'one';
    		alert(arrays.(foo));
    	}
    
    	test();
    </script>
    I've tried numerous variations of the alert line with evals, brackets and jQuery syntax but always seem to get the error:

    Code:
    XML filter is applied to non-XML value ({one:["1", "2", "3"], two:["a", "b", "c"]})
    Which makes me think I'm either attempting something stupid or only missing my target slightly.

    The code will be running within a jQuery project up if that helps in any way.
    Last edited by mattyod; 12-04-2009 at 03:53 PM.

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Code:
    alert(arrays[foo]);
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • Users who have thanked Kor for this post:

    mattyod (12-04-2009)

  • #3
    New Coder
    Join Date
    Feb 2005
    Posts
    97
    Thanks
    7
    Thanked 7 Times in 7 Posts
    Thanks man, syntax fail from me there

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    Simple JSON:
    http://json.org
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    That's Javascript square bracket notation which is linked in my sig.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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