zoukankan      html  css  js  c++  java
  • 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]
    
  • 相关阅读:
    洛谷 P1767 家族_NOI导刊2010普及(10)
    洛谷 P2919 [USACO08NOV]守护农场Guarding the Farm
    COGS 1619. [HEOI2012]采花
    UVA 11181 Probability|Given
    hdu 3336 Count the string
    洛谷 P2176 [USACO14FEB]路障Roadblock
    洛谷 P2691 逃离
    BZOJ 1040: [ZJOI2008]骑士
    vijos 1320 清点人数
    POJ 3417 Network
  • 原文地址:https://www.cnblogs.com/ajanuw/p/14184678.html
Copyright © 2011-2022 走看看