I currently have an object array which I sort and reduce like so: (The reason I used the below is that I found it in a post sort by alphabetical order keys in js)
const sortedData = Object.entries(templateData)
.sort(([,v1], [,v2]) => +v2 - +v1)
.reduce((r, [k, v]) => ({ ...r, [k]: v }), {});
This produces the following on a console.log(sortedData)
Aguascalientes
:
{productTemplate: {…}, colorVariants: false}
Alabama
:
{productTemplate: {…}, colorVariants: false}
Alaska
:
{productTemplate: {…}, colorVariants: false}
Arizona
:
{productTemplate: {…}, colorVariants: false}
Arizona
:
{productTemplate: {…}, colorVariants: false}
Arkansas
:
{productTemplate: {…}, colorVariants: false}
This is exactly what I want. However the list still appears as if it's NOT in alphabetical order.
When I do a forEach right after the above code like so:
Object.keys(sortedData).forEach((element, idx) => {
console.log(element)
console.log(idx)
});
The list appears in it's original order NOT the sorted reduced one. What am I missing here?