我一直在尝试在JavaScript中迭代多维数组,并打印数组中的每个元素。有没有不使用嵌套for循环打印多维数组中每个元素的方法?
http://jsfiddle.net/mKsDW/
var arr = [[1, 5],[7, 4]];
for(var i in arr){
alert(i); //this displays "0", then displays "1",
//instead of printing each element in the array
//how can I make it print each element in each 2D array instead,
//without using nested for-loops for each dimension of the array?
}
for...in
。是的,可以做到没有嵌套循环,但意义何在? 嵌套循环是最可读且全面的解决方案。其他解决方案只是抽象了一层迭代。 - Fabrício Matté