I have an array that is flat and already properly ordered. To display it in the UI, I need to sort it, by the placeId
, keeping the placeId
order, as well as the order of elements in the data
property.
const sortedData = [
{ id: 1, placeId: 12 },
{ id: 5, placeId: 12 },
{ id: 8, placeId: 31 },
{ id: 16, placeId: 31 },
{ id: 20, placeId: 45 },
{ id: 22, placeId: 45 },
]
// maintain order of place as well as data based on first appearance in sortedData
const uiPreparedData = [
{
place: 12,
data: [
{ id: 1, placeId: 12 },
{ id: 5, placeId: 12 },
],
},
{
place: 31,
data: [
{ id: 8, placeId: 31 },
{ id: 16, placeId: 31 },
],
},
{
place: 45,
data: [
{ id: 20, placeId: 45 },
{ id: 22, placeId: 45 },
],
},
]
Based on this approach I found the following solution containing lodash
. However, I cannot apply it to the nested structure of my case.
var uiPreparedData = _.chain(sortedData)
// Group the elements of Array based on `place` property
.groupBy("placeId")
.sortBy((item) => item.indexOf(item[0]))
// `key` is group's name (place), `value` is the array of objects
.map((value, key) => ({ place: key, data: value }))
.value();