其他分享
首页 > 其他分享> > 一个数组根据另一个数组排序

一个数组根据另一个数组排序

作者:互联网

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


标签:beijing,code,name,一个,shanghai,xian,数组,排序,shenzhen
来源: https://www.cnblogs.com/kebaoye/p/16338330.html