I am trying to loop through a following nested object and get an output as below:
const preference = {
    "ethnicity": {
        "value": "Newar",
        "rank": 1
    },
    "occupation": {
        "value": "Banker",
        "rank": 2
    }
}
I tried following:
let preferenceRank = {};
preference.map(pref => {
  preferenceRank[pref.rank] = pref; 
});
console.log(preferenceRank);
I get this error:
"TypeError: preference.map is not a function"...
Output required:
{
  1: "ethnicity",
  2: "occupation",
}






mapis an array function. You can useObject.keys()to iterate the keys and then get your rank values bypreference[key].