以下数组怎么按名称排序

{transferTotal: 4463838, touchTotal: 4463861, channelName: "其他", channelId: "OTHER"}

 {transferTotal: 10867488, touchTotal: 11260458, channelName: "支付宝", channelId: "ALIPAY"}

 {transferTotal: 1036820, touchTotal: 1094788, channelName: "微信", channelId: "WECHAT"}

需要按微信,支付宝,其他 来排序
我的办法很笨,就是遍历,如果是微信,push到新的数组里,把原数组的这个删了,在判断是支付宝,再删,有没有好办法

阅读 445
评论 2018-11-26 提问
    2 个回答
    asseek
    • 6.9k

    预设个排序数组,map一遍就是你需要的结果了

    let list = [
      {transferTotal: 4463838, touchTotal: 4463861, channelName: "其他", channelId: "OTHER"},
      {transferTotal: 10867488, touchTotal: 11260458, channelName: "支付宝", channelId: "ALIPAY"},
      {transferTotal: 1036820, touchTotal: 1094788, channelName: "微信", channelId: "WECHAT"}
    ];
    
    let result = ['微信', '支付宝', '其他'].map(name => list.find(item => item.channelName === name));
    
    console.log(result)
    评论 赞赏 2018-11-26