分形阵列语法

时间:2016-04-15 11:22:39

标签: javascript multidimensional-array

我有一个数组,每个数组中有4个值和6个数组,如下所示 -

[0,0,0,0,[] [],[],[],[],[]]

在每个数组中,我放置了此对象的副本,创建了这个 -

[0,0,0,0,[0,0,0,0,[] [],[],[],[],[]]],[[0,0,0, 0,],[],[],[],[],[]]],[[0,0,0,0,[] [],[],[],[],[]] ],[[0,0,0,0,[] [],[],[],[],[]]],[[0,0,0,0,[] [],[] ,[],[],[]]],[[0,0,0,0,[] [],[],[],[],[]]]]

我重复了每个阵列的过程。

而不是再次。

总共1 [i] + 6 [i] [i] + 36 [i] [i] [i] + 216 [i] [i] [i] + 1296 [i] [i] [i ] [I]

我的阵列正在做我想要的,是的,它工作正常,没有性能或任何问题!的 BUT !!!它看起来像这样 -

var Pl = [0,0,0,0,[0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]],

                     [0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]],

                     [0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]],

                     [0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]],

                     [0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]],

                     [0,0,0,0,[0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]],
                              [0,0,0,0,[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]],[0,0,0,0,[],[],[],[],[],[]]]]];

我可以这样做分配更小吗?并且维护存储对象值的能力'在每个阵列上(所有1500个)。使用脚本将其缩小会降低效率吗?

谢谢!

2 个答案:

答案 0 :(得分:1)

你可以像这样制作一个递归函数。



function createArrayWithDepth(depth) {
  if(depth == 0)
    return [];
  else {
    var subArray = createArrayWithDepth(depth - 1);
    return [0, 0, 0, 0, subArray, subArray, subArray, subArray, subArray, subArray];
  }
}

var array = createArrayWithDepth(3);

console.log(array);




答案 1 :(得分:1)

使用递归构建分形数组

// pattern
var p = [0, 0, 0, 0, [], [], [], [], [], [] ];

var res = (function fractal(a, depth) {
    return depth == 0 ? a: a.map(e => Array.isArray(e) ? fractal(p, depth-1) : e);
})(p, 2);


document.write(JSON.stringify(res, 0, 2));