Trying to arrange an array of objects to put in a table alphabetically whilst keeping a specific column as the first key/value pair in Javascript. It needs to stay as an array so I can map over it to render the table.
I've tried looping through the object and sorting it but cant seem to do it whilst keeping the 'category' as the first pair.
My data looks like this....
data = [{"Category": "Food",
"Dave": "286 / 91% / 98%",
"Caron": "215 / 83% / 87%",
"Sam": "134 / 100% / 100%",
"Edward": "433 / 100% / 100%",
"Adam": "827 / 100% / 100%"},
{"Category": "Drink",
"Dave": "286 / 91% / 98%",
"Elis": "215 / 83% / 87%",
"Creed": "134 / 100% / 99%",
"Edward": "433 / 100% / 100%",
"Tanya": "87 / 100% / 10%"}]
I have arranged the header alphabetically but am now trying to align the table body
This seems to have done the job....
sortTableRows = obj => {
const sortedObj = {};
sortedObj["Category"] = "";
Object.keys(obj)
.slice(1)
.sort()
.forEach(key => {
sortedObj[key] = obj[key];
});
return Object.keys(sortedObj);
};