第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

需要根據 Typescript / Angular 中嵌套對象的值對對象進行排序

需要根據 Typescript / Angular 中嵌套對象的值對對象進行排序

慕容3067478 2022-06-09 09:59:57
我正在努力在 covid-19 案例上制作一個 Angular 9 應用程序,我想根據嵌套對象的值對我的對象進行排序。這是我的數據,我想根據字段 s 的值按字母順序對 statewise 對象進行排序tate:kerela ,state:haryana ;{  "history": [    {      "day": "2020-03-14",      "total": {        "confirmed": 84      },      "statewise": [        {          "state": "Kerala",          "confirmed": 19        },        {          "state": "Assam",          "confirmed": 14        }      ]    },    {      "day": "2020-03-15",      "total": {        "confirmed": 104      },      "statewise": [        {          "state": "Kerala",          "confirmed": 119        },        {          "state": "Assam",          "confirmed": 114        }      ]    }  ]}所以排序后我想要我的數據排序數據{  "history": [    {      "day": "2020-03-14",      "total": {        "confirmed": 84      },      "statewise": [        {          "state": "Assam",          "confirmed": 14        },        {          "state": "Kerala",          "confirmed": 19        }      ]    },    {      "day": "2020-03-15",      "total": {        "confirmed": 104      },      "statewise": [        {          "state": "Assam",          "confirmed": 114        },        {          "state": "Kerala",          "confirmed": 119        }      ]    }  ]}我的 API 數據的鏈接在這里 API data API data link。我已經嘗試了兩天,我已經嘗試了這個答案,但仍然沒有得到我應該如何繼續(xù)。任何幫助都會很棒。
查看完整描述

2 回答

?
料青山看我應如是

TA貢獻1772條經驗 獲得超8個贊

以下代碼將有所幫助:


this.postService.getDetails().subscribe(

  response => {

    let sortedData= response.data.history

      .sort(function(a, b) {

        return new Date(b.date) - new Date(a.date);

      }); //Sort based on date


      sortedData.forEach(item=>item.statewise.sort(function(a, b) {

        if (a.state < b.state) {

          return -1;

        }

        if (a.state > b.state) {

          return 1;

        }

        return 0;

      })); //sort state wise in alphabetical order


    console.log(sortedData);

    this.loading = false;

  },

  error => {

    console.log(error);

    this.loading = false;

  }

);

示例演示:


https://stackblitz.com/edit/angular-rxjs-operators-examples?file=src%2Fapp%2Fstackoverflow%2Fmerge-map-error%2Fmerge-map-error.component.ts


https://angular-rxjs-operators-examples.stackblitz.io/stackoverflow


在控制臺中查找已排序的數據。


查看完整回答
反對 回復 2022-06-09
?
慕蓋茨4494581

TA貢獻1850條經驗 獲得超11個贊

您可以使用sort數組的原型來做到這一點。


let records = {

  "history": [

    {

      "day": "2020-03-14",

      "total": {

        "confirmed": 84

      },

      "statewise": [

        {

          "state": "Kerala",

          "confirmed": 19

        },

        {

          "state": "Assam",

          "confirmed": 14

        }

      ]

    },



    {

      "day": "2020-03-15",

      "total": {

        "confirmed": 104

      },

      "statewise": [

        {

          "state": "Kerala",

          "confirmed": 119

        },

        {

          "state": "Assam",

          "confirmed": 114

        }

      ]

    }

  ]

};


const res = {history: []};


res.history = records.history.map(history => {

  history.statewise.sort((a, b) => a.state < b.state ? -1 : 1);

    return history;

});


console.log(res);

.as-console-wrapper {

    min-height: 100% !important;

}


查看完整回答
反對 回復 2022-06-09
  • 2 回答
  • 0 關注
  • 83 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號