Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
514 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 Reply

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"]);
    });
   });
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...