sort Object based on 'weight' property _lodash

lodash sort Object

by theodore itzaris

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

JavaScript

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))