Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
Map with 2 different keys which depend on each other

I have an Array, project.users. In this I have different things of the user defined. Now I create a <p> Element for each of the usernames. (Multiple showing only once). Which is working. Project.users can have the same user listed several times, but each time with a different assigned role.

What I want: Show each username only once, as defined below, and show its associated user.role in the title attribut of the div. If the username is existing multiple times (user.given_name and user.family_name the same), it should also show the username only once, but (user.role always different in this case) should put both roles as the title in the div.

Example: username existing once:

Username only once

username existing twice:

Username twice

                         <% if (Array.isArray(project.users)) {
                             let usernames = project.users.map((user) => user.given_name + " " + user.family_name);
                             console.log(project.users);
                             for (username of new Set(usernames)) { %>
                                <div title="Here Comes The user.role">
                                 <p class="home_projectdata_content"><%= username %></p>
                                </div>
                    <%   }} %>
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

So You can create an object which has all roles associated to the key (username) in this case.

And you can just loop below <p> tag with roles[username]

           <% if (Array.isArray(project.users)) {
            let usernames = project.users.map((user) => user.given_name + " " + user.family_name);
            const roles = {};
            project.users.forEach((user) => {
                const username = user.given_name + " " + user.family_name;
                if (!roles[username]) roles[username] = [];
                roles[username].push( user.role);
            })

             console.log(project.users);
             for (username of new Set(usernames)) { %>
                <div title="Here Comes The user.role">
                  <p class="home_projectdata_content"><%= username %></p>
                  <% for(role of roles[username]) { %>
                     <span><%= role %></span>
                  <%  } %>
                </div>
     <%   }} %>

about 4 years ago · Juan Pablo Isaza Report

0

Firstly, I'm not sure what is the data source object you have (how is your fully JSON object or array data looks like). However, the idea you need to centralize the data source and transform it before rendering the UI.

For example, you might have projects object as below:

const projects = {
  users: [
    {
      id: 1,
      firstname: 'Ben',
      lastname: 'Truong',
      roles: {
        moderator: true,
        admin: true,
      }
    },
    {
      id: 2,
      firstname: 'Ana',
      lastname: 'Sarapova',
      roles: {
        moderator: true,
      }
    },
    {
      id: 3,
      firstname: 'Carlos',
      lastname: 'Hank',
      roles: {
        moderator: true,
      }
    },
  ]
};

const userInfo = [];

for (element of projects.users) {
//   console.log(element.roles);
  userInfo.push({
  id: element.id,
  fullName: element.firstname + ' ' + element.lastname,
  userRoles: Object.keys(element.roles).toString(),
  });
}; 

console.log(userInfo)

and the output looks like

[[object Object] {
  fullName: "Ben Truong",
  id: 1,
  userRoles: "moderator,admin"
}, [object Object] {
  fullName: "Ana Sarapova",
  id: 2,
  userRoles: "moderator"
}, [object Object] {
  fullName: "Carlos Hank",
  id: 3,
  userRoles: "moderator"
}]

Finally, you can map() through it as you want.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!