遍历数组数组 [英] looping through arrays of arrays

查看:33
本文介绍了遍历数组数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组数组(比如图形),如何迭代所有数组?

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]]
];

它只是一个示例数组,实际可以包含任意数量的数组,然后是数组.如何打印所有这些数字?它类似于 html 对象 DOM

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

推荐答案

这个递归函数应该可以处理任意数量的维度:

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);

这篇关于遍历数组数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆