Forum Moderators: open

Message Too Old, No Replies

JavaScript Array Pair Association - One / Red, Two / Blue, etc?

         

JAB Creations

9:47 am on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I need to be able to associate an array item with another array item. I'm not sure if I can determine the key number of the array item and then reference that same number to the second array...that was is the first approach I've thus far considered.

There are only two basic things I have to go by for this to work in my existing scripts; first I have to use an Array not an Object declaration and secondly I never use frameworks.

Suggestions please?

- John

Fotiman

2:48 pm on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Do you have two existing arrays, like the following:

var keys = ['one', 'two', 'three'];
var values = ['red', 'blue', 'green'];


And you need some way to map the value "one" to "red", "two" to "blue", and so on?

And am I correct that you can't create (or generate) a separate hash map object? For example:


var keys = ['one', 'two', 'three'];
var values = ['red', 'blue', 'green'];
var hashMap = {},
i,
n;
for (i = 0, n = keys.length; i < n; i++) {
hashMap[keys[i]] = values[i];
}
alert(hashMap['two']);

rocknbil

7:15 pm on Jun 6, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have to use an Array not an Object


With this restriction, the index of the array is all you've got, there are no associative arrays in JS as you probably know.

But . . . an object is far more cool, and probably, more efficient. Same as above example, in a different way . . .

var arr = {
'one':'red',
'two':blue',
'three':'green'
};
for (key in arr) {
alert('key: ' + key + ' val: ' + arr[key]);
}

JAB Creations

9:18 am on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well I can't remember the exact code but I remember what was the problem, referencing the object[0] would only give me the first letter instead of the first array item. However Fotiman's code works perfectly which is all that ultimately matters.

I've ready about array versus objects and I've tended to side with the use of objects though like the exact code I was having trouble with I don't exactly remember the reasons, LoL!

Just a small clarification, I presume the curly brackets replace the need to declare the object an object?

Rocknil, I tried your code out and it works great as a demo however you're missing a single quote in front of the blue value. ;)

Also the value list will actually store the names of functions that I'm not able to execute using the eval() method; if any one has any better recommendations I'm open to suggestions of course. :)

Thanks for both of your replies!

- John

Fotiman

2:37 pm on Jun 7, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I presume the curly brackets replace the need to declare the object an object?

Correct. Doing this:
var hashMap = {};
is declaring hashMap as an object literal.

JAB Creations

7:51 am on Jun 8, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for the clarification Fotiman! :)

- John

Drag_Racer

12:30 am on Jun 12, 2010 (gmt 0)

10+ Year Member



I'm not sure if I can determine the key number of the array item and then reference that same number to the second array.
Yes you can JAB. Use indexOf().

var array1 = ['one','two','three'];
var array2 = ['red','green','bue'];
array2[array1.indexOf('two')]; // outputs green

not sure of browser support so check on that before use.

JAB Creations

2:05 am on Jun 12, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Awesome, thanks Drag_Racer! :)

- John