Currently the response that is being sent back to client side looks like this:
[
{
"categoryName": "Orders",
"categoryItems": [
{
"group": "Group B",
"groupSettings": [
{
"settingName": "Group b description"
}
]
},
{
"group": "Group C",
"groupSettings": [
{
"settingName": "Group c description"
}
]
},
{
"group": "Group A",
"groupSettings": [
{
"settingName": "Group a description"
}
]
}
]
},
{
"categoryName": "Notifications",
"categoryItems": [
{
"group": "",
"groupSettings": [
{
"settingName": "Notification setting"
}
]
}
]
},
{
"categoryName": "Personalisation",
"categoryItems": [
{
"group": "",
"groupSettings": [
{
"settingName": "Personalisation setting"
}
]
}
]
}
]
However, the expected layout is:
[
{
"categoryName": "Personalisation",
"categoryItems": [
{
"group": "",
"groupSettings": [
{
"settingName": "Personalisation setting"
}
]
}
]
},
{
"categoryName": "Notifications",
"categoryItems": [
{
"group": "",
"groupSettings": [
{
"settingName": "Notification setting"
}
]
}
]
},
{
"categoryName": "Orders",
"categoryItems": [
{
"group": "Group A",
"groupSettings": [
{
"settingName": "Group a description"
}
]
},
{
"group": "Group b",
"groupSettings": [
{
"settingName": "Group b description"
}
]
},
{
"group": "Group c",
"groupSettings": [
{
"settingName": "Group c description"
}
]
}
]
}
]
How do I sort the categoryName
to match the order that I expected? I am thinking about matching the sort order to an array of object with categoryName
and sequence
properties, but I am stuck at the code.
My current code is as below:
const groupedData = _.chain(allData)
.groupBy('sectionName')
.map((allData, sectionName) => ({
categoryName: sectionName,
categorySettings: _.chain(allData)
.groupBy('group')
.map((groupSettings, group) => ({
group: group,
groupSettings: _.chain(groupSettings)
.sortBy('ordering')
.groupBy('title')
.map((titleSettings, title) => ({
settingName: title,
settingDescription: titleSettings[0].description,
settingInputs: _.map(titleSettings, ({name, value, inputSetting}) => ({
inputName: name,
inputValue: value,
inputConfig: inputSetting,
})),
}))
.value(),
}))
.value(),
}))
.value();
2
Answers
You can create an array with the expected order and use it to sort the groupedData array based on the categoryName
(or) a more type safe solution
You can create a mapping of
categoryName
to the required order i.e. index and then use that mapping to sort the data.I have reused your code. Considering you are using TS, i have made it typesafe too:
You can use whichever you suits you better (obviously by making it type safe)
I don’t know anything about lodash or Typescript, but here is a plain JavaScript solution that will place the elements with the listed properties first, in the custom-defined order, and any other elements (with
categoryNames
not mentioned in the lookup objectorder
) alphabetically ordered at the end.I used a string (
"00"
) for sorting instead of a numerical value as this allows for easy comparison with any non-numeric string. If, however, your array does not have these "other" properties you can simplify the comparison function by using numbers and simply subtracting one from the other.