按对象结果划分的Angularjs分割数组。

时间:2022-05-08 22:04:10

I have a array with objects like this:

我有这样一个对象数组:

{
  __v: 0,
  _id: "5835ced6ffb2476119a597b9",
  castvote: 1,
  time: "2016-11-23T17:16:06.676Z",
  userid: "57e0fa234f243f0710043f8f"
}

How can I make a new array that would filter them by the castvote - like an array containing the objects where castvote is 1, and one where castvote is 2?

如何创建一个新的数组来通过castvote对它们进行筛选——比如包含castvote为1、castvote为2的对象的数组?

3 个解决方案

#1


3  

You can do this in controller using $filter,

你可以在控制器中使用$filter,

$scope.castvoteOne = $filter('filter')($scope.results, {castvote: 1});
$scope.castvoteTwo = $filter('filter')($scope.results, {castvote: 2});

DEMO

演示

#2


1  

You can use Array.prototype.reduce and a hash table to group the array into an object with key as the castvote and value as the elements that have that particular castvote.

您可以使用Array.prototype。reduce和哈希表将数组分组为对象,其中键作为castvote,值作为具有该castvote的元素。

Now you can use result[castvote] to get the result for that particular castvote

现在您可以使用result[castvote]来获取特定castvote的结果。

See demo below:

请参见下面的演示:

var array=[{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:1,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"},{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:2,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"},{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:1,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"}]

var result = array.reduce(function(hash){
  return function(p,c) {
    if(!hash[c.castvote]) {
      hash[c.castvote] = [];
      p[c.castvote] = hash[c.castvote];
    }
    hash[c.castvote].push(c);
    return p;
  };
}(Object.create(null)),{});

// use result[castvote] to get the result for that castvote
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

#3


0  

As suggested here

在这里建议

You can use Underscore's groupby to do this generically by the type you want.

您可以使用下划线的groupby来按照您想要的类型进行这种通用操作。

_.groupBy($scope.results, "castvote")

#1


3  

You can do this in controller using $filter,

你可以在控制器中使用$filter,

$scope.castvoteOne = $filter('filter')($scope.results, {castvote: 1});
$scope.castvoteTwo = $filter('filter')($scope.results, {castvote: 2});

DEMO

演示

#2


1  

You can use Array.prototype.reduce and a hash table to group the array into an object with key as the castvote and value as the elements that have that particular castvote.

您可以使用Array.prototype。reduce和哈希表将数组分组为对象,其中键作为castvote,值作为具有该castvote的元素。

Now you can use result[castvote] to get the result for that particular castvote

现在您可以使用result[castvote]来获取特定castvote的结果。

See demo below:

请参见下面的演示:

var array=[{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:1,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"},{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:2,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"},{__v:0,_id:"5835ced6ffb2476119a597b9",castvote:1,time:"2016-11-23T17:16:06.676Z",userid:"57e0fa234f243f0710043f8f"}]

var result = array.reduce(function(hash){
  return function(p,c) {
    if(!hash[c.castvote]) {
      hash[c.castvote] = [];
      p[c.castvote] = hash[c.castvote];
    }
    hash[c.castvote].push(c);
    return p;
  };
}(Object.create(null)),{});

// use result[castvote] to get the result for that castvote
console.log(result);
.as-console-wrapper{top:0;max-height:100%!important;}

#3


0  

As suggested here

在这里建议

You can use Underscore's groupby to do this generically by the type you want.

您可以使用下划线的groupby来按照您想要的类型进行这种通用操作。

_.groupBy($scope.results, "castvote")