Jump to content

Javascript array pairing (two dimensional array)


Bl4ckMaj1k

Recommended Posts

Hello all!!! I'm back with a question...

 

So I would like to create two separate arrays and have them relate to one another in Javascript. For example:

 

var nameArray = new Array();

var urlArray = new Array();

 

nameArray = ['Jim', 'John', 'Chris', 'Smith'];

urlArray = ['google.com', 'yahoo.com', 'msn.com', 'gmail.com'];

 

I would like to pair these into a single array so I can click on Jim's name and get linked to google.com. I have tried several different approaches but so far nothing is working. I think I am just not understanding the overall concept of a multi-dimensional array. I don't understand how

 

newArray[0][0] = "'Jim', 'Google.com'"

 

would be data that I can use to create dynamic links depending on the place you are in the node. Please guys I could use a teacher right now....ready to be schooled LOL. As always, thanks in advance.

 

 

multi-dimensional array is an array within an array,

e.g.

var arr1 = [];
arr1[0] = [];
// now you can do arr1[0][0] = 'name'; arr[0][1] = 'url';

For your problem, you can use an array of objects,

e.g.

arr[0] = {name:'Joe', url:'google.com'};
// now you can do arr[0].name or arr[0]['name'] to get 'Joe'

Thanks nogray, I think I get what you mean. Can that be done in regular javascript? Because it almost looks like some form of jquery. Also, if you don't mind, can you elaborate on the example you provided. I am fairly new to Javascript.

 

Thanks again!

 

EDIT:

 

What I am asking is how I would take my example, add it to your example to retreive what will basically become a hyperlink dependent on its mate in the array...does that make any sense? I hope I am wording this right!! lol

All the examples I used are javascript, you can either loop through the array and check the values until you find the matching URL or you can create an object using the name as the key

e.g.

var obj = {};
obj['Jim'] = 'URL'; // or
obj.Jim = 'URL'; // if you use this style, make sure not to use any spaces or special characters in the name

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.