Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

json - Creating a valid server response from javascript/Angular

My JSON

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

Expected Object

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

i need to do a mapping based on the process date like in my expected object i have two dates which are the unique dates of all my JSON and in the next ids i have each id which belongs to that corresponding process dates right now i have been able to get the unique process dates but in the next ids i am not able to do can you please provide an example as to how i should loop through to achieve the same in angular

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You could use two nested forEach loops.

angular.forEach(obj.processDate, function(processValue, processIndex,date) {
  angular.forEach(obj2.RECORDS, function (recordValue, recordIndex,records) {
    if (processValue === recordValue["processDate"]) {
      if (!obj["Id"][processIndex]) {
        obj["Id"][processIndex]=[];
      }
      obj["Id"][processIndex].push(recordValue["Id"]);
    });
   });
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...