【问题标题】:Javascript array chunk with some rules带有一些规则的 Javascript 数组块
【发布时间】:2021-10-14 23:46:29
【问题描述】:

需要你的帮助。我有一个类似的数组;

const arr = 
  [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
  , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
  , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
  , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
  , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
  , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
  ]

如您所见,它们中的每一个都有不同的数据。我想创建像块数组这样的新数组。但它必须是;

**Maximum array length  = 2
**Order must be same 
**if array item length bigger than 3 or equal, it creates self array.
**if array item que does not begin with number, it create self array.

预期输出是;

const expArr = 
  [ [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
    , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
    ] 
  , [ { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } ] 
  , [ { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } ] 
  , [ { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
    , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
    ] 
  , [ { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } ] 
  ] 

我尝试了chunk分割数组的方法。

arr.reduce((prev, next)  => {
    if(Object.keys(next.item).length >= 3) {
        // Can be what
    }
    if(isNaN(next.que.split('')[0])) {
        // Can be what
    }
    
})

上面的函数仍然给我两个大小的数组,不适用规则

【问题讨论】:

    标签: javascript arrays chunks


    【解决方案1】:

    我是这样看的……

    const arr = 
      [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
      , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
      , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
      , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
      , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
      , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
      , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
      ]
    
    
    const maxSize = 2
    const itemMax = 3
    const result  = []
    let lastArr   = null
    
    for (let row of arr)
      {
      let len = Object.keys(row.item).length 
    
      if ((len >= itemMax) || isNaN(row.que.charAt(0)))
        {
        result.push([row])
        lastArr = null
        }
      else
        {
        if (!lastArr || lastArr.length >= maxSize)
          {
          lastArr = new Array()
          result.push(lastArr)
          }
        lastArr.push(row)
        }
      }
    
    console.log( result )
    .as-console-wrapper { max-height: 100% !important; top: 0 }

    【讨论】:

    • 如果 arr = , { item: { 0: 'asd' }, que: '3.question asdsad ... ' } 的第三个对象不起作用,它将添加到新数组而不是保持在一起
    • @JosephTroy 不,我的回答是正确的,你忘记了最大数组长度 = 2 的规则
    • 啊,是的。你是对的!
    【解决方案2】:

    基本上,您可以循环每个对象并在if 语句中确定该对象是添加到新数组中还是添加到现有数组中。在这个 sn-p 中更容易看到:

    const arr = 
      [ { item: { 0: 'asd' },                                     que: '1.question asdsad ... '    } 
      , { item: { 0: 'asd' },                                     que: '2.question asdsad ... '    } 
      , { item: { 0: 'asd', 1: 'asdsa', 2: 'asdsa', 3: 'asdsa' }, que: '3.question asdsad ... '    } 
      , { item: { 0: 'xx', 1: 'ssss', 2: 'ggg' },                 que: 'Question asdsad ... '      } 
      , { item: { 0: 'asd' },                                     que: '4.question asdsad ... '    } 
      , { item: { 0: 'asd' },                                     que: '5.sdd asdsad ... '         } 
      , { item: { 0: 'bbb', 1: 'bbb' },                           que: 'Dsad question asdsad ... ' } 
      ];
      
      let arNew = [];
      let arTmp = [];
      let sCurItemO = '';
      for(const obj of arr){
        if(obj.item['0'] != sCurItemO || arTmp.length == 2 || Object.keys(obj.item).length > 2 || isNaN(obj.que.substring(0, 1))){
          if(arTmp.length)
            arNew.push(arTmp)
          arTmp = [obj]
        }
        else
            arTmp.push(obj);
        sCurItemO = obj.item['0'];
      }
      arNew.push(arTmp);
      
      console.log(arNew)

    【讨论】:

    • 更新答案以允许最大数组长度 = 2
    猜你喜欢
    • 2011-09-15
    • 2018-08-29
    • 1970-01-01
    • 1970-01-01
    • 2019-10-26
    • 2013-02-28
    • 1970-01-01
    • 2010-12-15
    • 2015-02-08
    相关资源
    最近更新 更多