简体   繁体   中英

loop through nested object javascript

I am trying to loop through a following nested object and get an output as below:

const preference = {
    "ethnicity": {
        "value": "Gurung",
        "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",
}

You can use Object.entries to get keys and values at once (as array of arrays [key, value] ):

 const preference = { "ethnicity": { "value": "Gurung", "rank": 1 }, "occupation": { "value": "Banker", "rank": 2 } } const preferenceRank = {} for (const [key, { rank }] of Object.entries(preference)) { preferenceRank[rank] = key } console.log(preferenceRank)

(By the way, in your code it doesn't make any sense to use map there, since you are not mapping the array to anything, and you ignore the return value of map . You probably wanted forEach instead or, as I used now, a for loop.)

You can use the .entries() function to map over the object.

Object.entries(preference).reduce((out, [key, value]) => {
  out[value.rank] = key;
  return out;
},{});

Use Object.entries() to get an array of the keys and values of the object. You can then loop over that.

Use forEach if the loop is being done for side effect rather than using the values returned by the callback function.

 const preference = { "ethnicity": { "value": "Gurung", "rank": 1 }, "occupation": { "value": "Banker", "rank": 2 } } let preferenceRank = {}; Object.entries(preference).forEach(([pref, {rank}]) => { preferenceRank[rank] = pref; }); console.log(preferenceRank);

You could map the entries and build a new object.

 const preference = { ethnicity: { value: "Gurung", rank: 1 }, occupation: { value: "Banker", rank: 2 } }, result = Object.fromEntries(Object.entries(preference).map(([k, { rank }]) => [rank, k]) ); console.log(result);

This will work.

const preferenceRank = {};
Object.keys(preference).forEach((key) => {
  preferenceRank[preference[key]['rank']] = preference[key]['value'];
});

console.log(preferenceRank);

You could map over the keys and add to a result-object the rank/key-objects.

 const preference = { "ethnicity": { "value": "Gurung", "rank": 1 }, "occupation": { "value": "Banker", "rank": 2 } } let res= {}; Object.keys(preference).map((el,key) => { res[preference[el].rank] = el; }); console.log(res);

map only works for arrays, you are dealing with an object, what you can is go through the keys of the objects by using

Object.keys(preference)

this will return to you the object keys in an array as the following ["ethnicity","occupation"]

then you can map through it if you want and do your code

const preference = { "ethnicity": { "value": "Gurung", "rank": 1 }, "occupation": { "value": "Banker", "rank": 2 } } console.log({...Object.keys(preference)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM