如果我有一个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
};
当前回答
我已经用自己的方式尝试过了
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)
其他回答
对象按值排序(DESC)
function sortObject(list) {
var sortable = [];
for (var key in list) {
sortable.push([key, list[key]]);
}
sortable.sort(function(a, b) {
return (a[1] > b[1] ? -1 : (a[1] < b[1] ? 1 : 0));
});
var orderedList = {};
for (var i = 0; i < sortable.length; i++) {
orderedList[sortable[i][0]] = sortable[i][1];
}
return orderedList;
}
这是获取排序对象和返回排序对象的方法
let sortedObject = {}
sortedObject = Object.keys(yourObject).sort((a, b) => {
return yourObject[a] - yourObject[b]
}).reduce((prev, curr, i) => {
prev[i] = yourObject[curr]
return prev
}, {});
您可以根据您的需求自定义排序功能
感谢@orad在TypeScript中提供了答案。现在,我们可以在JavaScript中使用下面的代码片断。
function sort(obj,valSelector) { const sortedEntries = Object.entries(obj) .sort((a, b) => valSelector(a[1]) > valSelector(b[1]) ? 1 : valSelector(a[1]) < valSelector(b[1]) ? -1 : 0); return new Map(sortedEntries); } const Countries = { "AD": { "name": "Andorra", }, "AE": { "name": "United Arab Emirates", }, "IN": { "name": "India", }} // Sort the object inside object. var sortedMap = sort(Countries, val => val.name); // Convert to object. var sortedObj = {}; sortedMap.forEach((v,k) => { sortedObj[k] = v }); console.log(sortedObj); //Output: {"AD": {"name": "Andorra"},"IN": {"name": "India"},"AE": {"name": "United Arab Emirates"}}
我为此做了一个插件,它接受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
<pre>
function sortObjectByVal(obj){
var keysSorted = Object.keys(obj).sort(function(a,b){return obj[b]-obj[a]});
var newObj = {};
for(var x of keysSorted){
newObj[x] = obj[x];
}
return newObj;
}
var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
console.log(sortObjectByVal(list));
</pre>