I'm using Ramda to sort a normalized model object by key. Firstly I convert to key|value pairs (equivalent of Object.entries), and sort by the first value using R.head (the key value when in pairs)
I want to convert the resulting sorted array of pairs to an array of objects - in the example code I'm using ES6+ map and array destructuring
const data = {
2: {name: 'two'},
1: {name: 'one'}
}
const sortByKey = R.compose(R.sortBy(R.head), R.toPairs);
const sortedPairs = sortByKey(data)
console.log(sortedPairs)
// [["1",{"name":"one"}],["2",{"name":"two"}]]
const objs = sortedPairs.map(([key, value]) => {
return {[key]: value}
})
console.log(objs)
// [{"1":{"name":"one"}},{"2":{"name":"two"}}]
The part I can't find a Ramda function for is
const objs = sortedPairs.map(([key, value]) => {
return {[key]: value}
})