Jump to content

Javascript array pairing (two dimensional array)


Bl4ckMaj1k
 Share

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.

 

 

Link to comment
Share on other sites

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'

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.