简体   繁体   English

使用 Javascript 对 API JSON 响应进行排序

[英]Sorting API JSON response using Javascript

I am getting below response on hitting DoctorData.api and want to sort them all with their 'ID'.我在点击 DoctorData.api 时得到以下响应,并想用他们的“ID”对它们进行排序。 Can someone show me how to sort the Output JSON data and display it same format.有人可以告诉我如何对 Output JSON 数据进行排序并显示相同的格式。 Kindly Excuse my coding skills, this is my second test case.请原谅我的编码技能,这是我的第二个测试用例。 I am new to JS.我是 JS 新手。

 var doctorIDgeturl = geturl.geturls.getapiUrl; //'getapiUrl' is Doctor Get API var res = await api.getRequest(doctorIDgeturl); logger.logger().info('GET_data = ', JSON.stringify(res.data, null, 2)); var rescount = Object.keys(res.data.data.doctorList); //doctorList is the API response object for above GET API console.log("This is Sorted Id: "); const sortedResponse = sort(res.data, r => r.doctorListModels.associateId, ['asc']) //using ascending order to sort console.log(sortedResponse);

Current output:当前 output:

{
    "message": "Record Found",
    "data": {
        "DoctorsList": [
        {
            "id": "10",
            "name": "William",
            "launch_date": "2018-01-24T00:00:00.000-05:00"
        },
        {
            "id": "2",
            "name": "Snow",
            "launch_date": "2017-08-14T00:00:00.000-05:00"
        },
        {
            "id": "33",
            "name": "Thomas",
            "launch_date": "2018-11-29T00:00:00.000-05:00"
        },
        {
            "id": "3",
            "name": "Ismail",
            "launch_date": "2018-11-29T00:00:00.000-05:00"
        },
        {
            "id": "5",
            "name": "Jackson",
            "launch_date": "2018-04-10T00:00:00.000-05:00"
        }

Expected output after sorting:排序后预期的 output:

{
    "message": "Record Found",
    "data": {
        "DoctorsList": [

   {
        "id": "2",
        "name": "Snow",
        "launch_date": "2017-08-14T00:00:00.000-05:00"
    },
    {
        "id": "3",
        "name": "Ismail",
        "launch_date": "2018-11-29T00:00:00.000-05:00"
    },
    {
        "id": "5",
        "name": "Jackson",
        "launch_date": "2018-04-10T00:00:00.000-05:00"
    },
    {
        "id": "10",
        "name": "William",
        "launch_date": "2018-01-24T00:00:00.000-05:00"
    },
    {
        "id": "33",
        "name": "Thomas",
        "launch_date": "2018-11-29T00:00:00.000-05:00"
    }

parseInt(r.doctorListModels.associateId) or +r.doctorListModels.associateId parseInt(r.doctorListModels.associateId)+r.doctorListModels.associateId

it seems like it sorts the id as string not number似乎它将 id 排序为字符串而不是数字

Sorry I can't comment because of low reputation, but here is solution.抱歉,由于声誉低下,我无法发表评论,但这是解决方案。

 const obj = { "message": "Record Found", "data": { "DoctorsList": [{ "id": "10", "name": "William", "launch_date": "2018-01-24T00:00:00.000-05:00" }, { "id": "2", "name": "Snow", "launch_date": "2017-08-14T00:00:00.000-05:00" }, { "id": "33", "name": "Thomas", "launch_date": "2018-11-29T00:00:00.000-05:00" }, { "id": "3", "name": "Ismail", "launch_date": "2018-11-29T00:00:00.000-05:00" }, { "id": "5", "name": "Jackson", "launch_date": "2018-04-10T00:00:00.000-05:00" }] } } const sortedResponse = obj.data.DoctorsList.sort(function(a, b) { return parseInt(a.id) - parseInt(b.id) }); console.log(sortedResponse)

 const obj = { "message": "Record Found", "data": { "DoctorsList": [{ "id": "10", "name": "William", "launch_date": "2018-01-24T00:00:00.000-05:00" }, { "id": "2", "name": "Snow", "launch_date": "2017-08-14T00:00:00.000-05:00" }, { "id": "33", "name": "Thomas", "launch_date": "2018-11-29T00:00:00.000-05:00" }, { "id": "3", "name": "Ismail", "launch_date": "2018-11-29T00:00:00.000-05:00" }, { "id": "5", "name": "Jackson", "launch_date": "2018-04-10T00:00:00.000-05:00" } ] }} obj.data.DoctorsList = obj.data.DoctorsList.sort((a, b) => parseInt(a.id) > parseInt(b.id)); console.log(obj)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM