5

I'm currently fetching an array with .each:

$.each(messages, function(key,message){ doStuff(); });

But the key is the index of the array and not the associative key.

How can I get it easily?

5
  • 5
    An array doesn't have any associative key. The index is the key. What are you trying to do? Commented Jun 7, 2012 at 17:03
  • 2
    Where is you Array i.e messages? post that Commented Jun 7, 2012 at 17:04
  • 1
    Could you please provide an example array (2-3 elements) and what "associative key" you expect? Commented Jun 7, 2012 at 17:05
  • 1
    JavaScript doesn't have associative arrays. In JS you can use object to simulate that using square bracket notation, but you're adding properties. I believe you made the common mistake to have something like: var h = new Array(); h['foo'] = 'bar'. But you didn't added a new array's element, you just added a new property called 'foo'. In fact, alert(h.length) will be 0, and you will have alert(h.foo) equals to 'bar'. Commented Jun 7, 2012 at 17:06
  • "key is the index of the array and not the associative key". What do you mean? Array's don't have "associative keys". Can you show us what messages is, and what values you're looking for? Commented Jun 7, 2012 at 17:12

3 Answers 3

21

JavaScript doesn't have "associative arrays". It has arrays:

[1, 2, 3, 4, 5]

And objects:

{a: 1, b: 2, c: 3, d: 4, e: 5}

Array's don't have "keys". They have indices, which are counted starting at 0.

Arrays are accessed using [], and objects can be accessed using [] or ..

Example:

var array = [1,2,3];
array[1] = 4;
console.log(array); // [1,4,3]

var obj = {};
obj.test = 16;
obj['123'] = 24;
console.log(obj); // {test: 16, 123: 24}

If you try to access an array using a string as a key instead of an int, that may cause problems. You would be setting a property of the array and not a value.

var array = [1,2,3];
array['test'] = 4; // This doesn't set a value in the array
console.log(array); // [1,2,3]
console.log(array.test); // 4

jQuery's $.each works with both of these. In the callback for $.each, the first parameter, key, is either the object's key, or the array's index.

$.each([1, 2, 3, 4, 5], function(key, value){
    console.log(key); // Logs 0 1 2 3 4
});

$.each({a: 1, b: 2, c: 3, d: 4, e: 5}, function(key, value){
    console.log(key); // Logs 'a' 'b' 'c' 'd' 'e'
});
Sign up to request clarification or add additional context in comments.

1 Comment

Kudos for the neat JQuery each method!
10
var data = {
    val1 : 'text1',
    val2 : 'text2',
    val3 : 'text3'
};
$.each(data, function(key, value) {
    alert( "The key is '" + key + "' and the value is '" + value + "'" );
});
​

See the Demo

Comments

0

JavaScript doesn't have "associative arrays" as in PHP, but objects. Objects, though, may have string keys that corresponds to a value. Arrays are lists of values indexed numerically, so, if key is a number, it must be an array you are working with and not an object, and therefore you cannot get the key, as there is none.

Thus, you'd probably want to iterate over an array with a simple for-loop instead of a callback-based iterator such as $.each.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.