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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow using a string as a pointer to an array?

    So i've written up the code to do a lightbox esque overlay image gallery. everything is working smooth and fine. The only concern is that I have the following:

    lets say that I have a page that has multiple galleries. Obviously I would have to make each literal array with the gallery data different names, ex:

    Code:
    var _gallery1 = {
    	'images': [
    		{ 'id' : 1, 'src' : "" }
    	]
    }
    
    var _gallery2 = {
    	'images': [
    		{ 'id' : 1, 'src' : "" }
    	]
    }
    or would it be easier to do it multi dimensional?

    Code:
    var gallery_data = {
    	'gallery1': [
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	]
    	'gallery2': [
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	]
    }
    regardless of which direction I use... my main question is this:

    I have two variables which are the following:

    Code:
    galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null;
    galArray    = _gallery2.images;
    I'm unfamiliar with a way of doing something along the lines of this, and having it actually work:

    Code:
    galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null;
    galArray    = galVariable.images;
    obviously "galVariable" would be undefined... as i'm trying to get "_gallery2.images" but trying to use "galVariable" to point to "_gallery2"

    I'm familiar with solving a problem like this in PHP, not so much javascript. is there a JS function that can be used to ensure that it's using whatever "galVariable" equals to, and then that array's content is? Or is there a different way to go on about this.

    thanks in advance. if anyone needs some more clarification. please feel free to ask, and I'll respond asap.

  • #2
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    bump.......

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    Kansas, USA
    Posts
    487
    Thanks
    0
    Thanked 54 Times in 53 Posts
    I think I understand what you are asking. I had to read through a couple of times.

    Basically what you want is something like:

    When URL is www.something.com/#set=gallery1&photo=5
    That galArray would point to gallery1.images.

    When URL is www.something.com/#set=gallery2&photo=3
    That galArray would point to gallery2.images.

    Is this correct?

    Easy enough to do with the storage method you describe as multi-dimensional (really a nested object).

    Code:
    var gallery_data = {
    	'gallery1': {
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	},
    	'gallery2': {
    		'images': [
    			{ 'id' : 1, 'src' : "" }
    		]
    	}
    }
    
    galVariable = (i = location.hash.match(/#set=(.+)&photo=(\d+)/)) ? i[1] : null;
    galArray    = gallery_data[galVariable].images;
    You will probably want to check galVariable before attempting to assign galArray. Accessing gallery_data[null] will result in an error. Also, if the URL contained #set=foo, gallery_data["foo"] will be undefined.

    Note the curly brackets in the nested object. They were square brackets in your original post which should still work as an associative array, but will be a little more clear as an object. Also, don't forget the comma separating the objects, also in red.

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is exactly what i wanted. thank you!


  •  

    Tags for this Thread

    Posting Permissions

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