在javascript中对数组中的项目进行分组

时间:2018-04-10 18:16:18

标签: javascript arrays

我有一个项目数组,可以是具有许多属性的对象,我想根据顺序对其中一些项目进行分组:

示例:

[a,b,c,d,e] => [a,[b,c,d],e]
must group (b, c, d)  or (a) or (b, c) or (all)

must not group (a, c) or (a, d) for example because they are not sequential 

Some possibilites:

   [a,b,c,d]   to   [[a,b,c,d]]
   [a,b,c,d]   to   [[a],b,c,d]
   [a,b,c,d]   to   [[a,b],c,d]
   [a,b,c,d]   to   [[a,b,c],d]
   [a,b,c,d]   to   [a,b,[c,d]]

always sequential items
think of item a like an object with index;

2 个答案:

答案 0 :(得分:0)

如果我理解正确,您希望从索引n开始对数组的ind个顺序元素进行分组,并将该组放回到同一索引的数组中。您可以使用splice

来实现这一目标

function group(arr, ind, n) {
  var g = arr.splice(ind, n);      // cut out n elements from the array at index ind
  arr.splice(ind, 0, g);           // push back those cut-out elements into the same index ind (the elements are in array because of the first slpice call)
}


var arr = [1, 2, 3, 4, 5, 6];

group(arr, 2, 3);

console.log(arr); // [1, 2, [3, 4, 5], 6]

修改

根据要求,更改group以创建新数组,而不是更改原始数据并获取startend索引。因此,我们将使用slice / concat代替splice

function group(arr, start, end) {
  return [].concat(arr.slice(0, start), [arr.slice(start, end + 1)], arr.slice(end + 1));
}


var arr = [1, 2, 3, 4, 5, 6];

console.log(group(arr, 2, 3)); // => [1, 2, [3, 4, 5], 6]
console.log(group(arr, 4, 6)); // => [1, 2, 3, 4, [5, 6]]
console.log(group(arr, 0, 3)); // => [[1, 2, 3, 4], 5, 6]

汇总子阵列:从0start,从startend + 1(放入自己的数组,使其保持分组)和{{ 1}}向前。如果您希望end + 1索引为独占(end未包含的元素),则只需将end更改为end + 1

答案 1 :(得分:0)

根据最近的评论,您可能正在寻找Array自己的内置slice方法,它提供了数组的连续子区域,其中start和最终立场是它的论点:



function format(a){return "["+a.join(",")+"]";}

var arr=['a','b','c','d','e'];
console.log("original: "+format(arr));
console.log("slice(0,1): "+format(arr.slice(0,1)));
console.log("slice(0,2): "+format(arr.slice(0,2)));
console.log("slice(1,4): "+format(arr.slice(1,4)));
console.log("slice(2,4): "+format(arr.slice(2,4)));
console.log("slice(2,2): "+format(arr.slice(2,2)));



注1:第二个参数是" last + 1"的位置。你想要的元素,最好在最后一个例子中看到slice(2,2)提供一个空数组 注2:format只是将数组内容放在一行中,console.log(somearray)垂直显示元素。

当然,你可以做任何你需要的事情,但绝对值得注意的是,这个功能是存在的,它是内置的。
就像构建你描述的数组中的数组一样:



function format(a){var s="[",first=true;a.forEach(function(i){
  if(first)first=false;else s+=",";s+=i instanceof Array?format(i):i;});
  s+="]";return s;}

function group(arr,begin,end){
  var ret=[];
  for(var i=0;i<begin;i++)ret.push(arr[i]);
  ret.push(arr.slice(begin,end));
  for(var i=end;i<arr.length;i++)ret.push(arr[i]);
  return ret;
}

var arr=['a','b','c','d','e'];
console.log("original: "+format(arr));
console.log("group(arr,0,1): "+format(group(arr,0,1)));
console.log("group(arr,0,2): "+format(group(arr,0,2)));
console.log("group(arr,1,4): "+format(group(arr,1,4)));
console.log("group(arr,2,4): "+format(group(arr,2,4)));
console.log("group(arr,2,2): "+format(group(arr,2,2)));
&#13;
&#13;
&#13;
示例与slice - 解释相同,包括最后一个中的空数组(使用2,2)。如果您想要这样的空元素(如同一种光标&#39;),则取决于您。

当然,如果你想从数组中生成所有可能的选择,那么这个东西也适用于我原来答案中的嵌套循环 - 这就是你的问题最初在我看来:

&#13;
&#13;
function format(a){var s="[",first=true;a.forEach(function(i){
  if(first)first=false;else s+=",";s+=i instanceof Array?format(i):i;});
  s+="]";return s;}

function group(arr,begin,end){
  var ret=[];
  for(var i=0;i<begin;i++)ret.push(arr[i]);
  ret.push(arr.slice(begin,end));
  for(var i=end;i<arr.length;i++)ret.push(arr[i]);
  return ret;
}

var arr=['a','b','c','d','e'];
console.log("original: "+format(arr));
for(var i=0;i<arr.length;i++)
  for(var j=i+1;j<=arr.length;j++){
    console.log(format(group(arr,i,j)));
  }
&#13;
&#13;
&#13;