问题描述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"},]
2 回答
偶然的你
TA贡献1841条经验 获得超3个赞
意思就是按已交付>未开工>建设中的顺序排序吧letop={"已交付":0,"未开工":1,"建设中":2}letresultArr=newArr.sort((a,b)=>op[a.schedule]-op[b.schedule])
MMTTMM
TA贡献1869条经验 获得超4个赞
sort自定义一个排序函数就搞定了~letsortFunc=(a,b)=>{letlist=['已交付','未开工','建设中']returnlist.indexOf(a.schedule)-list.indexOf(b.schedule)}arr.sort(sortFunc)
添加回答
举报
0/150
提交
取消