重复数据删除JSON数组

时间:2016-06-26 23:09:15

标签: javascript angularjs json

如何为每个副本添加数组中的项目?

更改:

[
  {
     "uuid":"EE877ABD-932F-4B4C-AB23-B324363B0B60",
     "planning":[
                   {
                   "uuid":"6D680178-9B05-4744-A004-163D4B3E1E84",
                   "start":2016-01-01
                   },
                   {
                   "uuid":"37994EE3-F7E5-4199-A160-6D0B04D287D9",
                   "start":2016-01-02
                   }
                ]
  },
  {
     "uuid":"6BF57C74-F111-4901-A1C3-17E3B094C37E",
     "planning":[
                   {
                   "uuid":"21E386E9-0527-4EC2-81B5-A4F01B780B46",
                   "start":2016-03-01
                   },
                   {
                   "uuid":"D590A52C-DC9B-448E-A157-504CCA13FB45",
                   "start":2016-04-02
                   }
                ]
  }
]

到:

[
  {
     "uuid":"EE877ABD-932F-4B4C-AB23-B324363B0B60",
     "planning":{
                   "uuid":"6D680178-9B05-4744-A004-163D4B3E1E84",
                   "start":2016-01-01
                }
  },
  {
     "uuid":"EE877ABD-932F-4B4C-AB23-B324363B0B60",
     "planning":{
                   "uuid":"37994EE3-F7E5-4199-A160-6D0B04D287D9",
                   "start":2016-01-02
                }
  },
  {
     "uuid":"6BF57C74-F111-4901-A1C3-17E3B094C37E",
     "planning":{
                "uuid":"21E386E9-0527-4EC2-81B5-A4F01B780B46",
                   "start":2016-03-01
                }
  },
  {
     "uuid":"6BF57C74-F111-4901-A1C3-17E3B094C37E",
     "planning":{
                "uuid":"D590A52C-DC9B-448E-A157-504CCA13FB45",
                "start":2016-04-02
                }
  }
]

我在下面尝试了这个,但计划的值始终是第一项值。 此代码会删除当前计划项目的计划值。

var i= 0;
angular.forEach(data, function(value, key){
  if(value.planning.length>0){
    angular.forEach(value.planning, function(planningvalue, planningkey){
      $scope.out.push(value);
      $scope.out[i].planning=planningvalue;
      console.log($scope.out[i]);
      i=i+1;
    });
  }
});

由于

3 个答案:

答案 0 :(得分:2)

var res = [];

data.forEach(function(item) {
  var id = item.uuid;
  item.planning.forEach(function(plan) {
    res.push({ uuid: id, planning: plan});
  });
});

DEMO

答案 1 :(得分:1)

假设data数组包含您的原始数据,data2将包含处理后的数据。

var data2 = [];
data.forEach(function(item){
  item.planning.forEach(function(plan){
    var d = angular.copy(item); // item is outer object in main array. copy it and replace planning array with one of the containing objects.
    d.planning = plan;
    data2.push(d);
  });
});
console.log(data2);

答案 2 :(得分:1)

我认为这就是答案:



var data = [{
  "uuid": "EE877ABD-932F-4B4C-AB23-B324363B0B60",
  "planning": [{
    "uuid": "6D680178-9B05-4744-A004-163D4B3E1E84",
    "start": 2016 - 01 - 01
  }, {
    "uuid": "37994EE3-F7E5-4199-A160-6D0B04D287D9",
    "start": 2016 - 01 - 02
  }]
}, {
  "uuid": "6BF57C74-F111-4901-A1C3-17E3B094C37E",
  "planning": [{
    "uuid": "21E386E9-0527-4EC2-81B5-A4F01B780B46",
    "start": 2016 - 03 - 01
  }, {
    "uuid": "D590A52C-DC9B-448E-A157-504CCA13FB45",
    "start": 2016 - 04 - 02
  }]
}];

var result = data.reduce((c, d) =>
  c.concat(d.planning
    .map(p => Object.assign({}, d, {
      planning: p
    }))
  ), []
);

console.log(result)