What kind of mapping is this and where can i learn more about it

const plat1 = [{slug : ‘pc’},
{slug : ‘nintendo’},
{slug : ‘xbox’},
{slug : ‘nintendo’}]

const plat2 = {pc : ‘fawind’, nintendo : ‘fawind1’, xbox : ‘fawind3’, playstation : ‘fawind4’}

Now if run the command const plat3 = plat1.map(element => plat2[element.slug])

it gives me the out put {‘fawind’, ‘fawind1’, ‘fawind3’, ‘fawind4’}

just want to know what kid of mapping technique is this. I would like to learn more about this.

Thanks

Don’t know what I would call it. But you are using the values of the nested objects as the key for what we might call a HashMap when the object is used like that.

Not sure if any of this example code is useful or not.

const hashMap = { name: "Tom" };
const name = { firstName: hashMap["name"] };
console.log(name);
// { firstName: 'Tom' }


const allNames = [
  { name: "Tom" },
  { name: "Peter" },
  { name: "Jane" },
  { name: "Mary" },
]

const roles = { Tom: "admin", Peter: "user", Jane: "user", Mary: "admin" };

const rolesFromNames = allNames.map((item) => roles[item.name]);
console.log(rolesFromNames);
// [ 'admin', 'user', 'user', 'admin' ]

const usersWithRoles = allNames.map((item) => {
  return { ...item, role: roles[item.name] };
});

console.log(usersWithRoles);
// [ { name: 'Tom', role: 'admin' }, { name: 'Peter', role: 'user' }, { name: 'Jane', role: 'user' }, { name: 'Mary', role: 'admin' } ]

Thanks a ton @lasjorg . I owe you one.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.