2

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 trying

For every unique process dates i have i need the corresponding ids in a separate object in the below mentioned object the process date 24/04/2015 matches with the ids ending with 83,84,87 and the process date 27/04/2015 matches with the id ending with 54

My expected JSON object

{
    "processDate": [
        "2015/04/24",
        "2015/04/27"
    ],
    "Id": [
        [
            23040035705983,
            23040035705984,
            23040035705987
        ],
        [
            23070041800654
        ]
    ]
}

how i am 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 is with the id not mapping accordingly , i have created a plunker for the same (http://plnkr.co/edit/mkLXdOKayaqyDDDheFeu?p=preview) Any help would be appreciated.

3 Answers 3

1

Try this way: http://plnkr.co/edit/LS40YJR9X0OAqBTIP2lZ?p=preview

angular.forEach($scope.selectedItems.RECORDS, function ( item ) { 
            $scope.ProcessDate = item.processDate;
            var i=$scope.processDatesSelected.indexOf($scope.ProcessDate);
            if(i == -1){
              i=$scope.processDatesSelected.length;
              $scope.processDatesSelected.push($scope.ProcessDate);
              $scope.id[i]=[];
            }

            if(i != -1 && $scope.id[i].indexOf(item.Id) == -1 ){
                $scope.id[i].push(item.Id);
            }

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

Comments

0

You want to make array for each date, so you should take int account that date during id array creation

Check this: http://plnkr.co/edit/vrig4P9SgZh8Kk7BkyEt?p=preview

At first you must find item index from first array and then add your 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 also use structure like this. It could help you to easily maintanance this collection:

{
    "processDate": [
        { 
            Date: "2015/04/24",
            Ids: [
                23040035705983,
                23040035705984,
                23040035705987
            ]
        },
        {
            Date: "2015/04/27",
            Ids: [
                23070041800654
            ]
        }
    ]
}

Comments

0

This should do the trick:

var result = {
    processDate: [],
    Id: []
};

for(var i = 0; i < $scope.selectedItems.RECORDS.length; i++) {
    var record = $scope.selectedItems.RECORDS[i],
        index = result.processDate.indexOf(record.processDate);
    if(index === -1) {
        result.processDate.push(record.processDate);
        result.Id.push([record.Id]);
    } else {
        result.Id[index].push(record.Id);
    }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.