使用顺序for循环遍历关联数组 [英] Iterate through associative array using sequential for loop
本文介绍了使用顺序for循环遍历关联数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
array = {'key':'value'};我们有很多数据存储在关联数组中。
如何循环遍历数组,像这样使用普通循环而不是像这样的循环:
http://jsfiddle.net/HzLhe/
由于这个问题,我不想使用for-in:
在使用For(...在数组中)时出现问题
hasOwnProperty
。 示例:
var obj = {'key1':'value1','key2':'value2'};
(var index in obj){
if(!obj.hasOwnProperty(index)){
continue;
}
console.log(index);
console.log(obj [index]);
}
http://jsfiddle.net/jeffshaver/HzLhe/3/
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
解决方案
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/
这篇关于使用顺序for循环遍历关联数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文