Get the key of an associative array

arrays, javascript, jquery

Solution

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

Problem

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?

Original source