AngularJS-将JSON拆分为两个偶数组

AngularJS-将JSON拆分为两个偶数组,json,angularjs,Json,Angularjs,我得到了一份名单,名单的长度取决于员工的数量 $http({ url: '/SharedData/ClockInList', method: "POST", params: {Date: currentday} }).success(function (data) { }) 我需要一种在成功后将JSON拆分为两个长度相等的列表的方法。我想的是一种计算结果数量的方法,然后从0到$scope.a中的数字,剩下的放在$scope.B

我得到了一份名单,名单的长度取决于员工的数量

 $http({
        url: '/SharedData/ClockInList',
        method: "POST",
        params: {Date: currentday}
    }).success(function (data) {

    })
我需要一种在成功后将JSON拆分为两个长度相等的列表的方法。我想的是一种计算结果数量的方法,然后从0到$scope.a中的数字,剩下的放在$scope.B中

您可以使用来划分列表:

$scope.lotsOfData = [ 1, 2, 3, 4, 5, 6, 7, 8, 9 ];

$scope.listA = $scope.lotsOfData.filter( function(value,index){
    return index % 2 == 0;
});

$scope.listB = $scope.lotsOfData.filter( function(value,index){
    return index % 2 == 1;
});
或:

angular.module('MyModule',[])
.controller('MyController',函数($scope){
$scope.lotsOfData=[1,2,3,4,5,6,7,8,9];
$scope.listA=$scope.lotsOfData.filter(函数(值,索引){
返回索引%2==0;
});
$scope.listB=$scope.lotsOfData.filter(函数(值,索引){
返回索引%2==1;
});
$scope.listC=$scope.lotsOfData.slice(0,$scope.lotsOfData.length/2+1);
$scope.listD=$scope.lotsOfData.slice($scope.lotsOfData.length/2+1);
});

{{listA | | json}}

{{listB | | json}}

{{listC | | json}}

{{listD | | json}


您可以使用下划线.JS使用groupBy分割数据。 groupBy返回对象,如果您想转换为数组,请使用u.toArray()


这接近我需要的,但在ListA中我需要1,2,3,4,5,ListB将是6,7,8,9

您有一些选项,例如循环或使用过滤器/其他库进行分组,但您需要做的是获取项目的计数,然后将项目的索引=count/2用于第二个组

$http({
url:“/SharedData/ClockInList”,
方法:“张贴”,
参数:{Date:currentday}
}).成功(功能(数据){
group1=[];//这些需要在作用域、控制器或其他地方声明
第2组=[];
var count=data.length;
角度.forEach(数据、功能(项、键){

如果(键),这接近我需要的,但在ListA中,我需要1,2,3,4,5,ListB将是6,7,8,9
$scope.listC = $scope.lotsOfData.slice(0, $scope.lotsOfData.length / 2 + 1);

$scope.listD = $scope.lotsOfData.slice($scope.lotsOfData.length / 2 + 1);
var data = ["1", "7", "8", "9", "11", "12"];
var splitData = _.groupBy(data, function(element, index){
  return Math.floor(index/2);
});

splitData = _.toArray(splitData);
$http({
    url: '/SharedData/ClockInList',
    method: "POST",
    params: {Date: currentday}
}).success(function (data) {
    group1 = []; // these need to be declared on scope or controller or somewhere
    group2 = [];
    var count = data.length;
    angular.forEach(data, function(item, key) {
        if (key <= count / 2) {
            group1.push(item);
        } else {
            group2.push(item);
        }
    }
})