如果我有一个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 dayObj = {
"Friday":["5:00pm to 12:00am"] ,
"Wednesday":["5:00pm to 11:00pm"],
"Sunday":["11:00am to 11:00pm"],
"Thursday":["5:00pm to 11:00pm"],
"Saturday":["11:00am to 12:00am"]
}
我想按天排序,
我们应该先有daySorterMap,
var daySorterMap = {
// "sunday": 0, // << if sunday is first day of week
"Monday": 1,
"Tuesday": 2,
"Wednesday": 3,
"Thursday": 4,
"Friday": 5,
"Saturday": 6,
"Sunday": 7
}
初始化一个单独的对象sortedDayObj,
var sortedDayObj={};
Object.keys(dayObj)
.sort((a,b) => daySorterMap[a] - daySorterMap[b])
.forEach(value=>sortedDayObj[value]= dayObj[value])
你可以返回sortedDayObj
其他回答
var list = {
"you": 100,
"me": 75,
"foo": 116,
"bar": 15
};
var tmpList = {};
while (Object.keys(list).length) {
var key = Object.keys(list).reduce((a, b) => list[a] > list[b] ? a : b);
tmpList[key] = list[key];
delete list[key];
}
list = tmpList;
console.log(list); // { foo: 116, you: 100, me: 75, bar: 15 }
许多类似和有用的功能: https://github.com/shimondoodkin/groupbyfunctions/
function sortobj(obj)
{
var keys=Object.keys(obj);
var kva= keys.map(function(k,i)
{
return [k,obj[k]];
});
kva.sort(function(a,b){
if(a[1]>b[1]) return -1;if(a[1]<b[1]) return 1;
return 0
});
var o={}
kva.forEach(function(a){ o[a[0]]=a[1]})
return o;
}
function sortobjkey(obj,key)
{
var keys=Object.keys(obj);
var kva= keys.map(function(k,i)
{
return [k,obj[k]];
});
kva.sort(function(a,b){
k=key; if(a[1][k]>b[1][k]) return -1;if(a[1][k]<b[1][k]) return 1;
return 0
});
var o={}
kva.forEach(function(a){ o[a[0]]=a[1]})
return o;
}
以防万一,有人正在寻找保持对象(键和值),使用@Markus R和@James Moran注释的代码引用,只需使用:
var list = {"you": 100, "me": 75, "foo": 116, "bar": 15};
var newO = {};
Object.keys(list).sort(function(a,b){return list[a]-list[b]})
.map(key => newO[key] = list[key]);
console.log(newO); // {bar: 15, me: 75, you: 100, foo: 116}
下划线。js或Lodash.js用于高级数组或对象排序
var data = { "models": { "LTI": [ "TX" ], "Carado": [ "A", "T", "A(пасс)", "A(груз)", "T(пасс)", "T(груз)", "A", "T" ], "SPARK": [ "SP110C 2", "sp150r 18" ], "Autobianchi": [ "A112" ] } }; var arr = [], obj = {}; for (var i in data.models) { arr.push([i, _.sortBy(data.models[i], function(el) { return el; })]); } arr = _.sortBy(arr, function(el) { return el[0]; }); _.map(arr, function(el) { return obj[el[0]] = el[1]; }); console.log(obj); <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js" integrity="sha256-qXBd/EfAdjOA2FGrGAG+b3YBn2tn5A6bhz+LSgYD96k=" crossorigin="anonymous"></script>
试试这个。即使你的对象没有你试图排序的属性也会被处理。
只需通过发送属性和对象来调用它。
var sortObjectByProperty = function(property,object){
console.time("Sorting");
var sortedList = [];
emptyProperty = [];
tempObject = [];
nullProperty = [];
$.each(object,function(index,entry){
if(entry.hasOwnProperty(property)){
var propertyValue = entry[property];
if(propertyValue!="" && propertyValue!=null){
sortedList.push({key:propertyValue.toLowerCase().trim(),value:entry});
}else{
emptyProperty.push(entry);
}
}else{
nullProperty.push(entry);
}
});
sortedList.sort(function(a,b){
return a.key < b.key ? -1 : 1;
//return a.key < b.key?-1:1; // Asc
//return a.key < b.key?1:-1; // Desc
});
$.each(sortedList,function(key,entry){
tempObject[tempObject.length] = entry.value;
});
if(emptyProperty.length>0){
tempObject.concat(emptyProperty);
}
if(nullProperty.length>0){
tempObject.concat(nullProperty);
}
console.timeEnd("Sorting");
return tempObject;
}