将项目数组拆分为N个数组

时间:2022-06-27 02:42:20

I want to split a Array of numbers into N groups, which must be ordered from larger to smaller groups.

我想把一组数字分成N组,这必须从大到小的组排序。

For example, in the below code, split an Array of 12 numbers into 5 Arrays, and the result should be evenly split, from large (group) to small:

例如,在下面的代码中,将一个12个数字的数组分成5个数组,结果应该是均匀的,从大的(组)到小的:

[1,2,3] [4,5,6] [7,8] [9,10] [11,12]

Playground

// set up known variables
var arr = [1,2,3,4,5,6,7,8,9,10,11,12],
    numberOfGroups = 5,
    groups = [];

// split array into groups of arrays
for(i=0; i<arr.length; i++) {
  var groupIdx = Math.floor( i/(arr.length/numberOfGroups) );
  
  // if group array isn't defined, create it
  if( !groups[groupIdx] ) 
    groups[groupIdx] = [];
  // add arr value to group
  groups[groupIdx].push( arr[i] )
  
}

// Print result
console.log( "data: ", arr );
console.log( "groups: ", groups )


Update:

Thanks to SimpleJ's answer, I could finish my work.
The use case for this is an algorithm which splits HTML lists into "chunked" lists, a think which cannot be easily achieved by using CSS Columns.

多亏了SimpleJ的回答,我才能完成我的工作。这种情况的用例是一种算法,它将HTML列表分割成“分块”列表,这种想法通过使用CSS列是不容易实现的。

Demo page

2 个解决方案

#1


5  

I'm not 100% sure how this should work on different sized arrays with different group counts, but this works for your 12 digit example:

我不是百分之百地确定这应该如何在不同大小的数组中使用不同的组计数,但这适用于12位的示例:

function chunkArray(arr, chunkCount) {
  const chunks = [];
  while(arr.length) {
    const chunkSize = Math.ceil(arr.length / chunkCount--);
    const chunk = arr.slice(0, chunkSize);
    chunks.push(chunk);
    arr = arr.slice(chunkSize);
  }
  return chunks;
}



var arr = [1,2,3,4,5,6,7,8,9,10,11,12];
console.log( chunkArray(arr, 5) )

#2


0  

I think this is a more of a mathematical problem than a Javascript.

我认为这是一个比Javascript更复杂的数学问题。

const getGroups = (arr, noOfGroups) => {
  const division = Math.floor(arr.length / numberOfGroups);
  const groups = [[]];
  let remainder = arr.length % numberOfGroups;
  let arrIndex = 0;
  for (let i = 0; i < noOfGroups; i++) {
    for (let j = division + (!!remainder * 1); j >= 0; j--) {
      groups[i].push(arr[arrIndex]);
      arrIndex += 1;
    }
    remainder -= 1;
  }

  return groups;
};

const myGroups = getGroups([1,2,3,4,5,6,7,8,9,10,11,12], 5);

myGroups will be [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10], [11, 12]]

myGroups将[[1、2、3),(4、5、6),(7、8),(9、10),(11、12))

This will work for any number of groups and players

这将适用于任何数量的团队和玩家

#1


5  

I'm not 100% sure how this should work on different sized arrays with different group counts, but this works for your 12 digit example:

我不是百分之百地确定这应该如何在不同大小的数组中使用不同的组计数,但这适用于12位的示例:

function chunkArray(arr, chunkCount) {
  const chunks = [];
  while(arr.length) {
    const chunkSize = Math.ceil(arr.length / chunkCount--);
    const chunk = arr.slice(0, chunkSize);
    chunks.push(chunk);
    arr = arr.slice(chunkSize);
  }
  return chunks;
}



var arr = [1,2,3,4,5,6,7,8,9,10,11,12];
console.log( chunkArray(arr, 5) )

#2


0  

I think this is a more of a mathematical problem than a Javascript.

我认为这是一个比Javascript更复杂的数学问题。

const getGroups = (arr, noOfGroups) => {
  const division = Math.floor(arr.length / numberOfGroups);
  const groups = [[]];
  let remainder = arr.length % numberOfGroups;
  let arrIndex = 0;
  for (let i = 0; i < noOfGroups; i++) {
    for (let j = division + (!!remainder * 1); j >= 0; j--) {
      groups[i].push(arr[arrIndex]);
      arrIndex += 1;
    }
    remainder -= 1;
  }

  return groups;
};

const myGroups = getGroups([1,2,3,4,5,6,7,8,9,10,11,12], 5);

myGroups will be [[1, 2, 3], [4, 5, 6], [7, 8], [9, 10], [11, 12]]

myGroups将[[1、2、3),(4、5、6),(7、8),(9、10),(11、12))

This will work for any number of groups and players

这将适用于任何数量的团队和玩家