ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
[TOC] >[success] # 对象数组根据某个属性来进行排序 ~~~ 下面将一个'key顺序不对'的对象'数组对象'进行'排序' ~~~ JS ~~~ let arr = [ { "key": "003", "name": "悬索桥" }, { "key": "004", "name": "斜拉桥" }, { "key": "001", "name": "混凝土梁式桥" }, { "key": "002", "name": "钢梁桥" } ] // 排序方法 arr.sort((a, b) => b.key > a.key ? -1 : 1) console.log(arr) // [{"key":"001","name":"混凝土梁式桥"},{"key":"002","name":"钢梁桥"},{"key":"003","name":"悬索桥"},{"key":"004","name":"斜拉桥"}] ~~~