Javascript Array grouping category

2019-08-07 14:54发布

问题:

I have a problem with grouping an array of numeric value:

I have values in an array like this

var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];

that are then sorted into ascending numerical order

var sortedNumb = [1,1,2,3,4,4,5,5,6,6,7,7,8,10,11,12,15,15,18,20];

Now I want to create a group of numbers like

1-4 , 5-8 , 9-12 , 13-16 , 17-20

Is it possible to create groups dynamically, like that?

回答1:

Assuming that 1-4, 5-8, 9-12, 13-16, 17-20 grouping means that you want 5 groups, the first one (1-4) containing all the numbers within the [1, 4] interval; the second one (5-8) containing all the numbers within the [5, 8] interval, and so on.

// numbers and intervals must be in ascending order
var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
// 1-4 , 5-8 , 9-12 , 13-16 , 17-20
var intervals = [4, 8, 12, 16, 20];

numb.sort(function (a, b) {
  return a - b;
});

var groups = [];
var j = 0;
for (var i = 0; i < intervals.length; i++) {
  var group = [];
  while (numb[j] <= intervals[i]) {
    group.push(numb[j]);
    j++;
  }
  groups.push(group);
}
console.log(groups);

The output:

[ [ 1, 1, 2, 3, 4, 4 ],
  [ 5, 5, 6, 6, 7, 7, 8 ],
  [ 10, 11, 12 ],
  [ 15, 15 ],
  [ 18, 20 ] ]

EDIT: After reading the comment about calculating the intervals based on the max number in the array.

var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];

numb.sort(function (a, b) {
  return a - b;
});

var max = numb[numb.length - 1];
// Five groups based on the max value of the array
var increment = max / 5;
var groups = [];
var j = 0;
for (var i = increment; i <= max; i += increment) {
  var group = [];
  while (numb[j] <= i) {
    group.push(numb[j]);
    j++;
  }
  groups.push(group);
}
console.log(groups);

Output:

[ [ 1, 1, 2, 3, 4, 4 ],
  [ 5, 5, 6, 6, 7, 7, 8 ],
  [ 10, 11, 12 ],
  [ 15, 15 ],
  [ 18, 20 ] ]


回答2:

// important code

var numberToGroupOn = 4;

var numb = [5,10,11,6,7,18,1,8,2,1,15,12,4,5,3,4,6,7,15,20];
var srt = numb.slice(0).sort(function(a, b) { return a - b; });

var groupCount = Math.ceil(srt[srt.length-1] / numberToGroupOn);

var grps = {};

for(var i = 1; i <= groupCount; i++)
  {
    grps[((i*numberToGroupOn)-(numberToGroupOn-1)).toString() + '-' + (i*numberToGroupOn).toString()] = 
      srt.filter(function(a) {return (a <= i*numberToGroupOn) && (a >= (i*numberToGroupOn)-(numberToGroupOn-1))});
  }

// unimportant code to see output in SO snippet

var output = '';

for(var key in grps)
  {
    output += key + ': ' + grps[key]+'<br>';
  }

document.write(output);

This figures out the number of groups and then builds a dictionary of the groups using Array.prototype.filter.

It only works with positive numbers.



回答3:

Code : Assuming sortedNumb is not empty :)

var sortedNumb = [1,1,2,3,4,4,5,5,6,6,7,7,8,10,11,12,15,15,18,20];
var groups = [[sortedNumb[0]]];
var lastfirstValueInArray = sortedNumb[0];
var i = 1;
var j = 0;

while (i < sortedNumb.length)
{
    if (sortedNumb[i] >= lastfirstValueInArray+4 || (sortedNumb[i]%4 == 1 && sortedNumb[i] != lastfirstValueInArray)) 
    {
        j++;
        lastfirstValueInArray = 1+j*4;
        groups[j] = [];
    }
    groups[j][groups[j].length] = sortedNumb[i];
    i++;
}

console.log(groups);

Output :

[Array[6], Array[7], Array[3], Array[2], Array[2]]

Edit :

You seemed to want a range of group of 4, if you want N, just create a function taking it as parameter, and replace all 4 by N in code =)