将重复项组合到一个数组中

时间:2016-07-05 07:40:21

标签: javascript angularjs

我有一个如下所示的数组

[
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
]

我想将其转换为下面的

[
 {"st":"10:30,22:30","day":[1]},
 {"st":"9:30,20:30","day":[3,4]},
 {"st":"11:0,23:30","day":[7,6]},
 {"st":"8:0,23:0","day":[2,5]}
]

基于重复的“st”值,我想合并那些相应的日子。 我尝试了很多方法,但我无法做到。任何人都可以帮帮我。提前致谢。

3 个答案:

答案 0 :(得分:2)

一种方法如下:



var yourArray = [
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
];

var newArray =
Array.from(new Set(yourArray.map(elem => elem.st))) // get unique st's
    .map(elem => ({
        st: elem,
        day: yourArray.filter(el => el.st === elem).map(el => el.day) // get array of days
}));

console.log(newArray);




请注意,这使用了ES6功能,您需要使用转换器(例如Babel)在非ES6兼容的浏览器中使用它。

<强>资源

答案 1 :(得分:1)

这个基本代码可能会有所帮助。

angular.module('MyApp', [])
  .controller('TestCtrl', function($scope) {
    $scope.originalArray = [{
      "st": "10:30,22:30",
      "day": 1
    }, {
      "st": "9:30,20:30",
      "day": 3
    }, {
      "st": "9:30,20:30",
      "day": 4
    }, {
      "st": "11:0,23:30",
      "day": 7
    }, {
      "st": "11:0,23:30",
      "day": 6
    }, {
      "st": "8:0,23:0",
      "day": 2
    }, {
      "st": "8:0,23:0",
      "day": 5
    }];
    $scope.result = [];

    (function() {
      angular.forEach($scope.originalArray, function(value) {
        var locateIndex = _.findIndex($scope.result, {
          'st': value.st
        });
        if (locateIndex === -1) {
          var currentDayValue = value.day;
          value.day = [currentDayValue];
          $scope.result.push(value);
          return;
        }

        $scope.result[locateIndex].day.push(value.day);
      });
    }).call()
  });
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.13.1/lodash.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="MyApp" ng-controller="TestCtrl">
  original array
  <div>{{ originalArray }}</div>
  <br/>result
  <div>{{ result }}</div>
</div>

PS:我使用lodash作为JS实用程序库。 (findIndex帮助函数)

答案 2 :(得分:0)

创建临时数组以检查重复stindexOf。检查temp中是否存在st值,如果没有将日值更改为数组类型。如果存在推送日到先前的索引并在被splice

删除后
var object = [
{"st":"10:30,22:30","day":1},
{"st":"9:30,20:30","day":3},
{"st":"9:30,20:30","day":4},
{"st":"11:0,23:30","day":7},
{"st":"11:0,23:30","day":6},
{"st":"8:0,23:0","day":2},
{"st":"8:0,23:0","day":5}
];
var temp = [];
for(var i = object.length-1;i > 0;i--){
     if(temp.indexOf(object[i].st) != -1){
          var exist = temp.indexOf(object[i].st);
          object[exist].day.push(object[i].day);
          object.splice(i,1);
     }
     else{
         temp[i] = object[i].st;
         var d = object[i].day;
         object[i].day = [];
         object[i].day.push(d);
     }
}
console.log(object);