首页 > 编程语言 >javaScript一个数组对象根据另一个数组排序

javaScript一个数组对象根据另一个数组排序

时间:2023-01-30 18:14:00浏览次数:74  
标签:beijing code name javaScript xian shanghai 数组 排序 shenzhen

var list = [
    {name:'上海',code:'shanghai',},
    {name:'西安', code:'xian'},
    {name:'深圳', code:'shenzhen'},
    {name:'北京', code:'beijing'}
];
list.sort((a,b)=>{
    let order=['beijing','xian','shanghai','shenzhen'];
    return order.indexOf(a.code)-order.indexOf(b.code);
});
console.log(list);
/*
*排序后的结果
[
    {name: "北京", code: "beijing"},
    {name: "西安", code: "xian"},
    {name: "上海", code: "shanghai"} ,
    {name: "深圳", code: "shenzhen"}
]
*/

转载于csdn:丹哥不是哥

 

标签:beijing,code,name,javaScript,xian,shanghai,数组,排序,shenzhen
From: https://www.cnblogs.com/frenzy/p/17076872.html

相关文章