For those that already voted(negative), i show what i needed!
var obj = {
item_1: {name:'aaa',weight:4},
item_2: {name:'ddd',weight:2},
item_5: {name:'eee',weight:0},
item_3: {name:'ccc',weight:3},
item_6: {name:'ccc',weight:23},
item_4: {name:'eee',weight:1},
}
var arr = _.toPairs(obj)
console.log(arr)
var sortedArr = arr.sort(function(a,b){ return b[1].weight - a[1].weight})
console.log(sortedArr)
var sortedObj = _.fromPairs(sortedArr)
console.log(JSON.stringify(sortedObj))
live link here: sort Object based on 'weight'property
please study before you judge.
i have an object array like this:
var obj = {
item_1: {name:'aaa',weight:4},
item_2: {name:'ddd',weight:2},
item_3: {name:'ccc',weight:3},
item_4: {name:'eee',weight:1},
}
When i run: _.orderBy or _.sortBy()
e.g. : _.orderBy(obj,['weight'])
.
i get the sorted array , but without the initial keys
0: {name: "eee", weight: 1}
1: {name: "ddd", weight: 2}
2: {name: "ccc", weight: 3}
3: {name: "aaa", weight: 4}
But i need the original keys item_1, item_2 etc.
Can anyone give a hand ? Thanks.