Javascript 遍历数组数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7106410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
looping through arrays of arrays
提问by coure2011
I have an arrays of arrays (some thing like graph), How to iterate all arrays?
我有一个数组数组(像图这样的东西),如何迭代所有数组?
var parentArray = [
[[1,2,3],[4,5,6],[7,8,9]],
[[10,11,12],[13,14,15],[16,17,18]],
[[19,20,21],[22,23,24],[26,27,28]]
];
Its just an example array, actual can contains any number of array and then arrays. How to print all those numbers? Its similar to html objects DOM
它只是一个示例数组,实际可以包含任意数量的数组,然后是数组。如何打印所有这些数字?它类似于 html 对象 DOM
回答by Sascha Galley
This recursive function should do the trick with any number of dimensions:
这个递归函数应该可以处理任意数量的维度:
var printArray = function(arr) {
if ( typeof(arr) == "object") {
for (var i = 0; i < arr.length; i++) {
printArray(arr[i]);
}
}
else document.write(arr);
}
printArray(parentArray);
回答by Van Coding
For 2 dimenional Arrays:
对于二维数组:
for(var i = 0; i < parentArray.length; i++){
for(var j = 0; j < parentArray[i].length; j++){
console.log(parentArray[i][j]);
}
}
For arrays with an unknown number of dimensions you have to use recursion:
对于维数未知的数组,您必须使用递归:
function printArray(arr){
for(var i = 0; i < arr.length; i++){
if(arr[i] instanceof Array){
printArray(arr[i]);
}else{
console.log(arr[i]);
}
}
}
回答by Shane Van Wyk
what you need to do is something like this
你需要做的是这样的事情
var parentArray = [
[[1,2,3],[4,5,6],[7,8,9]],
[[10,11,12],[13,14,15],[16,17,18]],
[[19,20,21],[22,23,24],[26,27,28]]
];
for(int i = 0; i < parentArray.length;i++){
var value = parent[i];
for(int j = 0; j < parent[i].length; j++){
var innerValue = parent[i][j];
}
}
So this will be like a nested loop, then there where innerValue and value is you can do some operations, hope it helps
所以这就像一个嵌套循环,然后在innerValue和value所在的地方你可以做一些操作,希望它有帮助
回答by zhangsn
if you just want to print all the members,how about this?
如果你只是想打印所有成员,这个怎么样?
var items = parentArray.toString().split(",");
for(var i=0,j=items.length;i<j;i++)
console.log(items[i]);
回答by Saul
One option would be to use recursion which works with any number of dephts. See traverse()
, it's not tested but should give a rough idea:
一种选择是使用适用于任意数量 dephts 的递归。看traverse()
,它没有经过测试,但应该给出一个粗略的想法:
String.prototype.lpad = function(padString, length) {
var str = this;
while (str.length < length)
str = padString + str;
return str;
}
var level = 0;
function traverse(obj) {
if (obj instanceof Array) { level++; for(var i in obj) traverse(obj[i]); level--; }
else console.log(''.lpad('-', level) + obj);
}
回答by laurent
If you have a DOM like structure, you need to recursively iterate through the elements. Something like that should work:
如果你有一个类似 DOM 的结构,你需要递归地遍历元素。这样的事情应该工作:
function printArray(array) {
for (var i = 0; i < array.length; i++) {
var v = array[i];
if (v instanceof Array) {
printArray(v);
} else {
console.log(v);
}
}
}
回答by yoozer8
You would use nested for loops here. The outer loop would iterate the parent array, giving you one of the internal arrays each time. The inner loop would give you the items within each array. Example:
您将在这里使用嵌套的 for 循环。外循环将迭代父数组,每次都为您提供一个内部数组。内部循环将为您提供每个数组中的项目。例子:
for(childArray in parentArray){
for(item in childArray){
//do stuff here to each number
}
}
回答by Anil_M
Using array.forEachmethod
使用array.forEach方法
parentArray.forEach( function(childArray) {
childArray.forEach(function(item){
console.log(item);
});
});
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ]
[ 10, 11, 12 ]
[ 13, 14, 15 ]
[ 16, 17, 18 ]
[ 19, 20, 21 ]
[ 22, 23, 24 ]
[ 26, 27, 28 ]
One liner using => "fat arrow" in ES6+
在 ES6+ 中使用 => "fat arrow" 的一个衬垫
parentArray.forEach(subarray => { subarray.forEach( item => {console.log(item); }); });
[ 1, 2, 3 ]
[ 4, 5, 6 ]
[ 7, 8, 9 ]
[ 10, 11, 12 ]
[ 13, 14, 15 ]
[ 16, 17, 18 ]
[ 19, 20, 21 ]
[ 22, 23, 24 ]
[ 26, 27, 28 ]
If you want to list individual number in item, add another layer of forEach.
如果要在 item 中列出单个编号,请添加另一层 forEach。