【问题标题】:JS Split array into X chunks, then Y chunks and so onJS 将数组拆分为 X 块,然后是 Y 块,依此类推
【发布时间】:2019-10-12 11:43:52
【问题描述】:

我怎样才能将一个数组分成 6 个块,然后是 3 个块,然后是 6 个块,然后是 3 个块等等?

假设我有这个数据集:

const datasaet = [
  { text: 'hi1' },
  { text: 'hi2' },
  { text: 'hi3' },
  { text: 'hi4' },
  { text: 'hi5' },
  { text: 'hi6' },
  { text: 'hi7' },
  { text: 'hi8' },
  { text: 'hi9' },
  { text: 'hi10' },
  { text: 'hi11' },
  { text: 'hi12' },
  { text: 'hi13' },
  { text: 'hi14' },
  { text: 'hi15' },
  { text: 'hi16' },
]

我需要将它拆分成这样的数组:

const expected = [
  [
    { text: 'hi1' },
    { text: 'hi2' },
    { text: 'hi3' },
    { text: 'hi4' },
    { text: 'hi5' },
    { text: 'hi6' },
  ],
  [
    { text: 'hi7' },
    { text: 'hi8' },
    { text: 'hi9' },
  ],
  [
    { text: 'hi10' },
    { text: 'hi11' },
    { text: 'hi12' },
    { text: 'hi13' },
    { text: 'hi14' },
    { text: 'hi15' },
    { text: 'hi16' },
  ]
]

基本上我所做的是将数组拆分为 6 个块(如果是事件)和 3 个块(如果是奇数)。

但是我不知道该怎么做。我目前的尝试看起来是这样的,我可以将它完美地分成 6 块,但我该如何去做接下来的 3 块,然后是接下来的 6 块,依此类推:

const grouped = datasaet.reduce(
  (initital: any[], current, index, items) => {
    const isFirstOfSix = index % 6 === 0
    if (isFirstOfSix) {
      const nextSix = items.slice(index, index + 6)
      initital.push(nextSix)
    }

    return initital
  },
  []
) 

【问题讨论】:

    标签: javascript arrays typescript grouping


    【解决方案1】:

    您可能会考虑创建数组的副本(以避免改变原始数组),然后拼接出项目直到它为空,检查并切换一个布尔值,指示在当前迭代中是删除 6 个还是 3 个项目:

    const datasaet = [
      { text: 'hi1' },
      { text: 'hi2' },
      { text: 'hi3' },
      { text: 'hi4' },
      { text: 'hi5' },
      { text: 'hi6' },
      { text: 'hi7' },
      { text: 'hi8' },
      { text: 'hi9' },
      { text: 'hi10' },
      { text: 'hi11' },
      { text: 'hi12' },
      { text: 'hi13' },
      { text: 'hi14' },
      { text: 'hi15' },
      { text: 'hi16' },
    ]
    
    const tempArr = datasaet.slice();
    const output = [];
    let removeSix = true;
    while (tempArr.length) {
      output.push(tempArr.splice(0, removeSix ? 6 : 3));
      removeSix = !removeSix;
    }
    console.log(output);

    【讨论】:

    • 谢谢一百万,这就像一个魅力。不过,快速的问题是,当您切换删除六标志时,该行代码被调用了多少次?只是出于好奇
    • 取决于输入的长度。如果长度为 1-6,则一次。如果长度为 7-9,则两次。如果长度为 10-15,三次等。
    【解决方案2】:

    您可以创建一个接收数组和块大小数组的函数。该函数迭代数组,在块大小之间循环,并使用 slice 从原始数组中获取当前块大小:

    const chunks = (arr, chunkSize) => {
      const result = [];
      
      let current = -1;
    
      for (let i = 0; i < arr.length; i += chunkSize[current]) {
        current = (current + 1) % chunkSize.length;
        
        result.push(arr.slice(i, i + chunkSize[current]));
      }
    
      return result;
    }
    
    const dataset = [{"text":"hi1"},{"text":"hi2"},{"text":"hi3"},{"text":"hi4"},{"text":"hi5"},{"text":"hi6"},{"text":"hi7"},{"text":"hi8"},{"text":"hi9"},{"text":"hi10"},{"text":"hi11"},{"text":"hi12"},{"text":"hi13"},{"text":"hi14"},{"text":"hi15"},{"text":"hi16"}];
    
    const result = chunks(dataset, [6, 3]);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-06
      • 1970-01-01
      • 2021-06-15
      • 2013-06-27
      相关资源
      最近更新 更多