I have an Object containing nested Arrays of Objects that I need to sort alphabetically by the "label" property and move the object with the label "other" to the end of each Array. I have a working solution, but I'm looking for a more streamlined approach. You can view my current solution on this JSFiddle link.
let things = {
"animals": [{
"id": 0,
"label": "cat"
},
{
"id": 100,
"label": "Undefined"
},
{
"id": 200,
"label": "turtle"
},
{
"id": 300,
"label": "Other"
},
],
"colors": [{
"id": 0,
"label": "yellow"
},
{
"id": 100,
"label": "green"
},
{
"id": 200,
"label": "red"
},
{
"id": 300,
"label": "blue"
}
]
}
let sortedThings = {};
Object.entries(things).forEach((entry) => {
let key = entry[0];
let otherOption = entry[1].filter((o)=> o.label === "Other");
console.log(otherOption)
let vals = entry[1].sort((a, b) => (a.label > b.label) ? 1 : ((b.label > a.label) ? -1 : 0)).filter((s) => s.label !== "Undefined")
if(otherOption.length){
vals = vals.filter((s) => s.label !== "Other");
vals = vals.concat(otherOption);
}
})