Utilizing Ramda
to organize a normalized model object by key involves converting it to key|value pairs (similar to Object.entries
) and sorting by the first value using R.head
(which represents the key
when in pairs)
The goal is to transform the resulting sorted array of pairs into an array of objects. In the code snippet below, ES6+ map
and array destructuring
are used for this purpose
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 challenge lies in finding a suitable Ramda
function for the following task:
const objs = sortedPairs.map(([key, value]) => {
return {[key]: value}
})