Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
36
rated 0 times [  40] [ 4]  / answers: 1 / hits: 16100  / 11 Years ago, thu, april 4, 2013, 12:00:00

I have a lot of data stored in associative array.



array = {'key':'value'};


How to loop throught an array like this using an normal for loop and not a loop like here:
http://jsfiddle.net/HzLhe/



I don't want to use for-in because of this problems:
Mootools when using For(...in Array) problem


More From » arrays

 Answers
16

As others have pointed out, this isn't an array. This is a JavaScript object. To iterate over it, you will have to use the for...in loop. But to filter out the other properties, youw ill have to use hasOwnProperty.



Example:



var obj={'key1': 'value1','key2':'value2'};

for (var index in obj) {
if (!obj.hasOwnProperty(index)) {
continue;
}
console.log(index);
console.log(obj[index]);
}


http://jsfiddle.net/jeffshaver/HzLhe/3/


[#79124] Tuesday, April 2, 2013, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kourtney

Total Points: 368
Total Questions: 103
Total Answers: 85

Location: Bonaire
Member since Sat, May 1, 2021
3 Years ago
;