Home > Enterprise >  How to get 2 properties from map function
How to get 2 properties from map function

Time:10-05

I want to get 2 properties from an array using the map function. I know it is possible to get 1 property, but I am wanting to get 2 and then display in a string.

var us1 = { name: "[email protected]", role: "QA" }; us2 = { name: "[email protected]", role: "CFO" }; us3 = { name: "[email protected]", role: "CCO" };
var users = [us1, us2, us3];
// here In am getting the user only - i want to also get the role
let user = users.map((i: any) => i.user && i.role);
// put user and role in a string
let result = user.join(" and ");

CodePudding user response:

That all depends on how you want those values returned. For example, you can return them as objects in your new array:

row.SignedBy.map((i: any) => ({ user: i.user, role: i.role}))

Of course then your subsequent use of join() doesn't make sense, because user is now an array of objects and not strings. If you want them as concatenated strings, build the string you want to return:

row.SignedBy.map((i: any) => `${i.user} ${i.role}`)

In that case you'd format that string however you want.

Either way, the overall point is that your callback function to .map() simply needs to return whatever you want each element in the resulting array to be. An object, a formatted string, something else, etc.

CodePudding user response:

return [i.user, i.role] then you can spread -> [...user]

  • Related