my json object
$scope.selecteditems ={ "records": [ { "id": 23040035705987, "arriveddate": "2015/04/24", "expirationdate": null, "replaceddate": null, "processdate": "2015/04/24" }, { "id": 23070041800654, "arriveddate": "2015/04/24", "expirationdate": null, "replaceddate": null, "processdate": "2015/04/27" }, { "id": 23040035705984, "arriveddate": "2015/04/24", "expirationdate": null, "replaceddate": null, "processdate": "2015/04/24" }, { "id": 23040035705983, "arriveddate": "2015/04/24", "expirationdate": null, "replaceddate": null, "processdate": "2015/04/24" } ] }
what trying
for every unique process dates have need corresponding ids in separate object in below mentioned object process date 24/04/2015 matches ids ending 83,84,87 , process date 27/04/2015 matches id ending 54
my expected json object
{ "processdate": [ "2015/04/24", "2015/04/27" ], "id": [ [ 23040035705983, 23040035705984, 23040035705987 ], [ 23070041800654 ] ] }
how trying
angular.foreach($scope.selecteditems.records, function ( item ) { $scope.processdate = item.processdate; if($scope.processdatesselected.indexof($scope.processdate) == -1){ $scope.processdatesselected.push($scope.processdate); } if($scope.processdatesselected.indexof($scope.processdate) != -1 && $scope.id.indexof(item.id) == -1 ){ $scope.id.push(item.id); } }); $scope.changesselected.push({processdate:$scope.processdatesselected,ids:$scope.id}); console.log(json.stringify($scope.changesselected));
issue id not mapping accordingly , have created plunker same (http://plnkr.co/edit/mklxdokayaqydddhefeu?p=preview) appreciated.
you want make array each date, should take int account date during id
array creation
check this: http://plnkr.co/edit/vrig4p9sgzh8kk7bkyet?p=preview
at first must find item index first array , add element on proper position.
var processdateindex = $scope.processdatesselected.indexof($scope.processdate); if(processdateindex != -1 && $scope.id.indexof(item.id) == -1 ){ if ($scope.id.length < processdateindex+1) { $scope.id.push([]); } $scope.id[processdateindex].push(item.id); }
you can use structure this. maintanance collection:
{ "processdate": [ { date: "2015/04/24", ids: [ 23040035705983, 23040035705984, 23040035705987 ] }, { date: "2015/04/27", ids: [ 23070041800654 ] } ] }
Comments
Post a Comment