I'm sorry for my poor English.
groups: [
{
id: 1,
smallGroups: [
{name: "vegetable"},
{name: "meat"}
]
},
{
id: 2,
smallGroups: [
{name: "car"},
{name: "train"}
]
}
]
From the above array, I am trying to sort the names alphabetically by the associative array directly under groups.
let arr = [];
groups.forEach(element => {
arr = element.smallGroups.sort((a,b) => {
let textA = a.name.toUpperCase();
let textB = b.name.toUpperCase();
return (textA < textB) ? 1 : (textA > textB) ? -1 : 0;
})
});
I was able to sort them alphabetically, but I cannot store all the arrays from forEach in an array called arr.
If there is a better way to do this, please let me know.
Array.prototype.mapinstead ofArray.prototype.forEach.sortis inplace, sogroupswill reflect the change after the loop is finished. No need to havearrat all. If you really want it inarr, then doarr = groupsafter the loop... but that looks like not very useful.