您好,我想按给定的字符串对对象数组(Json Response)进行排序。我通过HTTP服务获得以下响应。
{
"result": "success",
"reason": "Operation completed successfully.",
"payload": [
{
"product_code": null,
"indigent_kit": 0,
"quan_limit": 0,
"sort_order": 1040,
"menu_prod_cat": "CLTH",
"header_text": "CLOTHING",
"product_desc": "CLOTHING",
"macs_menu_id": "1047-2000000366",
"total_price": "0"
},
{
"indigent_kit": 0,
"quan_limit": 6,
"sort_order": 1010,
"product_desc": "TUBE SOCKS - 1 PAIR",
"menu_prod_cat": "CLTH",
"product_code": "9000",
"macs_menu_id": "1047-2000000366",
"total_price": "2.05",
"header_text": null
},
{
"indigent_kit": 0,
"quan_limit": 6,
"sort_order": 1030,
"product_desc": "T-SHIRT - SMALL",
"menu_prod_cat": "CLTH",
"product_code": "9005",
"macs_menu_id": "1047-2000000366",
"total_price": "3.8",
"header_text": null
},
{
"indigent_kit": 0,
"quan_limit": 6,
"sort_order": 1060,
"product_desc": "T-SHIRT - LARGE",
"menu_prod_cat": "CLTH",
"product_code": "9015",
"macs_menu_id": "1047-2000000366",
"total_price": "3.8",
"header_text": null
},
{
"indigent_kit": 0,
"quan_limit": 6,
"sort_order": 1050,
"product_desc": "T-SHIRT - XL",
"menu_prod_cat": "CLTH",
"product_code": "9020",
"macs_menu_id": "1047-2000000366",
"total_price": "3.8",
"header_text": null
},
],
"locked": false,
"api": "get_menus"
}
现在,我想通过sort_order
键以打字稿的角度对这个响应进行排序。
有什么想法吗?谢谢
答案 0 :(得分:1)
您好,您只需在javascript中使用函数Array.sort
aka
const sortedarray = payload.sort((a,b) => a.sort_order - b.sort_order)
或者对于您的公共变量
public dataSource = new MatTableDataSource<any>([]); this.dataSource.data = response.payload.filter(d => d.product_code != null && d.macs_menu_id === this.InmateMacMenu).sort((a,b) => a.sort_order - b.sort_order);
答案 1 :(得分:0)
尝试使用lodash
代替
_.sortBy(data.payload, [({ sort_order }) => sort_order]);