Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 04-26-2011, 08:08 PM   PM User | #1
moimikey
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
moimikey is an unknown quantity at this point
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.
moimikey is offline   Reply With Quote
Old 04-27-2011, 04:59 PM   PM User | #2
moimikey
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
moimikey is an unknown quantity at this point
bump.......
moimikey is offline   Reply With Quote
Old 04-27-2011, 10:17 PM   PM User | #3
kansel
Regular Coder

 
Join Date: Jul 2002
Location: Kansas, USA
Posts: 465
Thanks: 0
Thanked 45 Times in 44 Posts
kansel is on a distinguished road
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.
kansel is offline   Reply With Quote
Old 04-27-2011, 10:19 PM   PM User | #4
moimikey
New to the CF scene

 
Join Date: Apr 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
moimikey is an unknown quantity at this point
this is exactly what i wanted. thank you!
moimikey is offline   Reply With Quote
Reply

Bookmarks

Tags
array, javascript, json, literal array

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:54 PM.


Advertisement
Log in to turn off these ads.