Bl4ckMaj1k Posted September 3, 2011 Share Posted September 3, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/246374-javascript-array-pairing-two-dimensional-array/ Share on other sites More sharing options...
nogray Posted September 3, 2011 Share Posted September 3, 2011 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' Quote Link to comment https://forums.phpfreaks.com/topic/246374-javascript-array-pairing-two-dimensional-array/#findComment-1265182 Share on other sites More sharing options...
Bl4ckMaj1k Posted September 3, 2011 Author Share Posted September 3, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246374-javascript-array-pairing-two-dimensional-array/#findComment-1265223 Share on other sites More sharing options...
nogray Posted September 4, 2011 Share Posted September 4, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/246374-javascript-array-pairing-two-dimensional-array/#findComment-1265237 Share on other sites More sharing options...
Bl4ckMaj1k Posted September 6, 2011 Author Share Posted September 6, 2011 Totally get this now!!! Javascript is SUCH a powerful language. Sometimes with all the capabilities floating around I get a tad-bit confuzzled!! lol thanks for clearing things up! Solved. Bl4ck Maj1k Quote Link to comment https://forums.phpfreaks.com/topic/246374-javascript-array-pairing-two-dimensional-array/#findComment-1265815 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.