如果我有一个JavaScript对象,如:
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
是否有一种方法可以根据值对属性进行排序?最后得到
list = {
"bar": 15,
"me": 75,
"you": 100,
"foo": 116
};
如果我有一个JavaScript对象,如:
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
是否有一种方法可以根据值对属性进行排序?最后得到
list = {
"bar": 15,
"me": 75,
"you": 100,
"foo": 116
};
当前回答
谢谢你,继续回答@Nosredna
现在我们知道对象需要转换为数组,然后对数组排序。这对于按字符串排序数组(或转换对象为数组)非常有用:
Object {6: Object, 7: Object, 8: Object, 9: Object, 10: Object, 11: Object, 12: Object}
6: Object
id: "6"
name: "PhD"
obe_service_type_id: "2"
__proto__: Object
7: Object
id: "7"
name: "BVC (BPTC)"
obe_service_type_id: "2"
__proto__: Object
//Sort options
var sortable = [];
for (var vehicle in options)
sortable.push([vehicle, options[vehicle]]);
sortable.sort(function(a, b) {
return a[1].name < b[1].name ? -1 : 1;
});
//sortable => prints
[Array[2], Array[2], Array[2], Array[2], Array[2], Array[2], Array[2]]
0: Array[2]
0: "11"
1: Object
id: "11"
name: "AS/A2"
obe_service_type_id: "2"
__proto__: Object
length: 2
__proto__: Array[0]
1: Array[2]
0: "7"
1: Object
id: "7"
name: "BVC (BPTC)"
obe_service_type_id: "2"
__proto__: Object
length: 2
其他回答
另一个关于Object的例子。值、sort()和展开运算符。
var paintings = {
0: {
title: 'Oh my!',
year: '2020',
price: '3000'
},
1: {
title: 'Portrait V',
year: '2021',
price: '2000'
},
2: {
title: 'The last leaf',
year: '2005',
price: '600'
}
}
我们使用object .values将对象转换为对象数组:
var toArray = Object.values(paintings)
然后我们对数组进行排序(按年份和价格),使用spread操作符使原始数组不可变,并使用sort()方法对数组进行排序:
var sortedByYear = [...toArray].sort((a, b) => a.year - b.year)
var sortedByPrice = [...toArray].sort((a, b) => a.price - b.price)
最后,我们生成新的排序对象(同样,使用展开运算符来保持以[x: number]为键的对象的object的原始形式):
var paintingsSortedByYear = {
...sortedByYear
}
var paintingsSortedByPrice = {
...sortedByPrice
}
希望这对你有所帮助!
我用sort的解决方案:
let list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
let sorted = Object.entries(list).sort((a,b) => a[1] - b[1]);
for(let element of sorted) {
console.log(element[0]+ ": " + element[1]);
}
我已经用自己的方式尝试过了
var maxSpeed = {
car: 300,
bike: 60,
motorbike: 200,
airplane: 1000,
helicopter: 400,
rocket: 8 * 60 * 60
};
var sorted = {}
Object.keys(maxSpeed).sort ((a,b) => maxSpeed[a] - maxSpeed[b]).map(item => sorted[item] = maxSpeed[item]);
console.log(sorted)
let toSort = {a:2323, b: 14, c: 799}
let sorted = Object.entries(toSort ).sort((a,b)=> a[1]-b[1])
输出:
[ [ "b", 14 ], [ "c", 799 ], [ "a", 2323 ] ]
我为此做了一个插件,它接受1个arg,这是一个未排序的对象,并返回一个对象,已排序的道具值。这将适用于所有二维对象,如{"Nick": 28, "Bob": 52}…
var sloppyObj = {
'C': 78,
'A': 3,
'B': 4
};
// Extend object to support sort method
function sortObj(obj) {
"use strict";
function Obj2Array(obj) {
var newObj = [];
for (var key in obj) {
if (!obj.hasOwnProperty(key)) return;
var value = [key, obj[key]];
newObj.push(value);
}
return newObj;
}
var sortedArray = Obj2Array(obj).sort(function(a, b) {
if (a[1] < b[1]) return -1;
if (a[1] > b[1]) return 1;
return 0;
});
function recreateSortedObject(targ) {
var sortedObj = {};
for (var i = 0; i < targ.length; i++) {
sortedObj[targ[i][0]] = targ[i][1];
}
return sortedObj;
}
return recreateSortedObject(sortedArray);
}
var sortedObj = sortObj(sloppyObj);
alert(JSON.stringify(sortedObj));
下面是该函数按预期工作的演示 http://codepen.io/nicholasabrams/pen/RWRqve?editors=001