AngularJS:使用过滤器将数组拆分为数组

时间:2016-05-19 11:57:42

标签: javascript arrays angularjs function filter

我想将一组对象拆分成多个数组。 这些字符串在AngularJS中完美无缺,并将整个数组arr按照我想要的方式分成四个数组:

    var arr0 = $filter('filter')(arr, { filterVal: 0 }, true);
    var arr1 = $filter('filter')(arr, { filterVal: 10 }, true);
    var arr2 = $filter('filter')(arr, { filterVal: 50 }, true);
    var arr3 = $filter('filter')(arr, { filterVal: 60 }, true);

实际上,总数上面的四个数组是原始数组(因为每个项目中的filterVal总是等于0, 10, 50 or 60)。

那么,问题是如何使上面的代码更短?所以,我需要在一行中将arr拆分为new_arr,以便

new_arr[0] is equal to arr0,
new_arr[1] is equal to arr1,
new_arr[2] is equal to arr2,
new_arr[3] is equal to arr3.

换句话说,我想使用一串代码(如果可能)而不是四个字符串。是否可以使用AngularJS?

这样我会让它变得更有活力(不要坚持四个)。

谢谢。

刚添加: 例如,

    arr = [
        {"code":"kfk", "flag":"dfmk", "filterVal": 0},
        {"code":"asg", "flag":"sdg3", "filterVal": 50},
        {"code":"asdf", "flag":"34t", "filterVal": 10},
        {"code":"hsdfh", "flag":"dfsg43", "filterVal": 50},
        ];

预计将被纳入

var new_arr[0] = [{"code":"kfk", "flag":"dfmk", "filterVal": 0}];//0s
var new_arr[1]= [{"code":"asdf", "flag":"34t", "filterVal": 10},];//10s
var new_arr[2]= [{"code":"asg", "flag":"sdg3", "filterVal": 50},{"code":"hsdfh", "flag":"dfsg43", "filterVal": 50}];//50s
var new_arr[3]= [];//60s

3 个答案:

答案 0 :(得分:1)

创建自定义过滤器:

app.filter('groupify', function(){
  return function(list, key) {
    return list.reduce((carry, row) => {
      if (!carry[row[key]]) carry[row[key]] = [];
      carry[row[key]].push(row);
      return carry;
    }, {});
  };
});

并使用它:

 var new_arr = $filter('groupify')(arr, 'filterVal');

你会得到一个对象:{0: [...], 10: [...], 50: [...], 60: [...]}
如果需要,可以很容易地将其转换为数组。

答案 1 :(得分:1)

您可以使用普通的Javascript和数组进行分组。



var array = [{ "code": "kfk", "flag": "dfmk", "filterVal": 0 }, { "code": "asg", "flag": "sdg3", "filterVal": 50 }, { "code": "asdf", "flag": "34t", "filterVal": 10 }, { "code": "hsdfh", "flag": "dfsg43", "filterVal": 50 }, ],
    groups = [{ filterVal: 0 }, { filterVal: 10 }, { filterVal: 50 }, { filterVal: 60 }],
    result = groups.map(function (a) { return []; });

array.forEach(function (a) {
    groups.some(function (b, i) {
        var k = Object.keys(b)[0];
        return a[k] === b[k] && result[i].push(a);
    });
});

console.log(result);




或者您可以使用更线性的方法



var array = [{ "code": "kfk", "flag": "dfmk", "filterVal": 0 }, { "code": "asg", "flag": "sdg3", "filterVal": 50 }, { "code": "asdf", "flag": "34t", "filterVal": 10 }, { "code": "hsdfh", "flag": "dfsg43", "filterVal": 50 }, ],
    filter = [{ filterVal: 0 }, { filterVal: 10 }, { filterVal: 50 }, { filterVal: 60 }],
    groups = Object.create(null),
    result = filter.map(function (a) { return []; });

filter.forEach(function (b, i) {
    groups[b[Object.keys(b)[0]]] = i;
});
array.forEach(function (a) {
    result[groups[a.filterVal]].push(a);
});

console.log(result);




答案 2 :(得分:1)

这个怎么样?

var new_arr = [0, 10, 50, 60].map(function (n) { 
    return $filter('filter')(arr, { filterVal: n }, true); 
});