var yourListMaps = [];
var a = {quantity: 10, otherAttr: 'tmp1'};
var b = {quantity: 20, otherAttr: 'tmp2'};
var c = {quantity: 30, otherAttr: 'tmp3'};
yourListMaps.push(a);
yourListMaps.push(b);
yourListMaps.push(c);
如果你想按数量排序,你可以:
// Sort c > b > a
yourListMaps.sort(function(a,b){
return b.quantity - a.quantity;
});
或者
// Sort a > b > c
yourListMaps.sort(function(a,b){
return a.quantity - b.quantity;
});
const map = new Map();
map.set("key","value");
map.set("key","value");
const object = Object.keys(map.sort().reduce((a,b) => (a[k] = map[a], b), {});
console.log(object);
//This will work to sort a map by key.