思路: js的Object对象类型,不能直接排序,不过Array是可以排序的
将Object类型的key,转为Array排序,再将结果转为Object
示例
let data = {
name: "tom",
age: 20,
};
console.log(data);
// { name: 'tom', age: 20 }
// key排序
let sortKeys = Object.keys(data).sort();
console.log(sortKeys);
// [ 'age', 'name' ]
// 按照key的顺序,重新生成新的object
let sortKeyData = {};
for (let key of sortKeys) {
sortKeyData[key] = data[key];
}
console.log(sortKeyData);
// { age: 20, name: 'tom' }
标签:name,age,Object,js,let,key,升序,data
From: https://blog.51cto.com/mouday/5965915