求大佬指点哈!js数组对象排序问题求指导!

问题描述
js数组对象排序问题
问题出现的环境背景及自己尝试过哪些方法
js数组对象排序问题
相关代码
//请把代码文本粘贴到下方(请勿用图片代替代码)初始代码:letnewArr=[
{"schedule":"已交付","0":"3005B00000041"},
{"schedule":"未开工","0":"3005B00000308"},
{"schedule":"建设中","0":"3005B00000037"},
{"schedule":"建设中","0":"3005B00000283"},
{"schedule":"未开工","0":"3005B00000304"}
];
你期待的结果是什么?实际看到的错误信息又是什么?
目标结果:letresultArr=[
{"schedule":"已交付","0":"3005B00000041"},
{"schedule":"未开工","0":"3005B00000308"},
{"schedule":"未开工","0":"3005B00000304"},
{"schedule":"建设中","0":"3005B00000037"},
{"schedule":"建设中","0":"3005B00000283"},
]
慕雪6442864
浏览 312回答 2
2回答

慕丝7291255

意思就是按已交付>未开工>建设中的顺序排序吧letop={"已交付":0,"未开工":1,"建设中":2}letresultArr=newArr.sort((a,b)=>op[a.schedule]-op[b.schedule])

慕姐8265434

sort自定义一个排序函数就搞定了~letsortFunc=(a,b)=>{letlist=['已交付','未开工','建设中']returnlist.indexOf(a.schedule)-list.indexOf(b.schedule)}arr.sort(sortFunc)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript