• 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]
    
  • 相关阅读:
    过去式和过去进行式
    现在式和现在进行式
    英文文法的最基本规则
    Vue 标签中的ref属性和refs
    APICloud
    小程序
    React 传值 组件传值 之间的关系
    css clip样式 属性功能及作用
    小程序点击预览 为什么显示空白
    小程序
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14184678.html
Copyright © 2020-2023  润新知