vendredi 1 juillet 2016

deduplicate JSON Array

How to add item in array for each duplicate ?

Change :

[
  {
     "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
                   }
                ]
  }
]

to :

[
  {
     "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
                }
  }
]

I have tried this below , but planning's value is always first item value. This code remove planning'value with the current planning item.

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;
    });
  }
});

Thanks

Aucun commentaire:

Enregistrer un commentaire