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 i am striving for
My goal is to separate the IDs corresponding to unique processing dates into a new object. For example, the processing date 24/04/2015 matches with the IDs ending with 83, 84, 87, and the processing date 27/04/2015 matches with the ID ending with 54.
My desired JSON object
{
"processDate": [
"2015/04/24",
"2015/04/27"
],
"Id": [
[
23040035705983,
23040035705984,
23040035705987
],
[
23070041800654
]
]
}
my approach
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 arises with the ID mappings, I have created a Plunker for reference (http://plnkr.co/edit/mkLXdOKayaqyDDDheFeu?p=preview) Any assistance would be greatly appreciated.