0

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

3 Answers 3

2
var res = [];

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

DEMO

Sign up to request clarification or add additional context in comments.

Comments

1

I Think this is the answer:

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)

Comments

1

Let say, data array contains your orignal data and data2 will contains data after processing.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.