我正在尝试用_orderBy()进行排序和数组,它有一个不幸的副产品,即吹走顶级数组键!我需要能够按这些顶级键以及子键进行排序(它做得很好)。bigArray = { "22H7564": { // <------ these are the top level keys "name":"Thor", "age:"600", "height":187, }, "20H3211": { "name":"Mesa", "age:"42", "height":120, }, "27H4506": { "name":"Jesper", "age:"122", "height":143, },}这是排序但无聊的结果():_orderBy(BigArray, 'name', 'desc')bigArray = { "0": { // <------ FRAAACK! i need these! "name":"Jesper", "age:"122", "height":143, }, "1": { "name":"Mesa", "age:"42", "height":120, }, "2": { "name":"Thor", "age:"600", "height":187, },}有没有办法对这些顶级键进行排序和保留?我也需要能够对这些进行排序。
2 回答
侃侃无极
TA贡献2051条经验 获得超10个赞
那是因为你实际上是一个对象,没有数组。将其定义为数组,即可获得所需的结果。bigArray[]
bigArray = [{
"22H7564": { // <------ these are the top level keys
"name":"Thor",
"age":"600",
"height":187,
},
"20H3211": {
"name":"Mesa",
"age":"42",
"height":120,
},
"27H4506": {
"name":"Jesper",
"age":"122",
"height":143,
},
}]
console.log(_.orderBy(bigArray, 'name', 'desc'));
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
添加回答
举报
0/150
提交
取消