js 增加数组的嵌套层数

class A {}

const proxy = new Proxy(new A(), {
  get(o, k) {
    if (!/d+/.test(k.toString())) return o[k];

    if (!o.prototype) o.prototype = {};
    if (!o.prototype.list) o.prototype.list = [];
    o.prototype.list.push(parseInt(k));
    return proxy;
  },
});

proxy[3][4][3];

let count = proxy.prototype.list.reduce((acc, it) => (acc *= it), 1);

const r = [];
for (let i = 0; i < count; i++) r.push(i);

console.log(r);
// (36) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35]


function unflattenDeep(arr, list) {
  for (let i = list.length - 1; i >= 1; i--) {
    const value = list[i];
    arr = arr.reduce((acc, it, index) => {
      if (index % value === 0) acc.push([]);
      acc[acc.length - 1].push(it);
      return acc;
    }, []);
  }
  return arr;
}


// proxy.prototype.list => [3,4,3]
console.log(unflattenDeep(r, proxy.prototype.list));

// (3) [Array(4), Array(4), Array(4)]
//   0: Array(4)
//     0: (3) [0, 1, 2]
//     1: (3) [3, 4, 5]
//     2: (3) [6, 7, 8]
//     3: (3) [9, 10, 11]
//   1: Array(4)
//     0: (3) [12, 13, 14]
//     1: (3) [15, 16, 17]
//     2: (3) [18, 19, 20]
//     3: (3) [21, 22, 23]
//   2: Array(4)
//     0: (3) [24, 25, 26]
//     1: (3) [27, 28, 29]
//     2: (3) [30, 31, 32]
//     3: (3) [33, 34, 35]
原文地址:https://www.cnblogs.com/ajanuw/p/14184678.html