Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

111
Visualizações
How to search both object properties

Very novice coder here and I'm currently learning JavaScript. I'm trying to run an Object.keys() code to return the 'skills' of the people in the users Object and find who has more skills based on the result. When I run my code I keep getting "Uncaught TypeError: Cannot read properties of undefined (reading 'skills')." I have no clue how to access the values of the skills key without searching each user individually. Here's the code I'm working with:

const users = {
    Alex: {
      email: 'alex@alex.com',
      skills: ['HTML', 'CSS', 'JavaScript'],
      age: 20,
      isLoggedIn: false,
      points: 30
    },
    Asab: {
      email: 'asab@asab.com',
      skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 
      'Node'],
      age: 25,
      isLoggedIn: false,
      points: 50
    },
}
const names = Object.keys(users)
const userSkills = Object.keys(users.names.skills)
console.log(userSkills)

//The above doesn't work, however this does:
const alexSkills = Object.keys(users.Alex.skills)
console.log(alexSkills) //3
const asabSkills = Object.keys(users.Asab.skills)
console.log(asabSkills) //8

(console.log(Object.keys(users.Alex.Skills))
//alternate format I've used, but I'm asked to do the former.)

Is there a way to access and display the skills key in which I don't have to search each property individually to find them? I know I'll get the answer with the individual search, but doing this for an object with a huge number of properties would take far too long and I'd like to learn a quicker method. Any help is appreciated!

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is a lots of way to do it and let's use Object.keys and a single for loop to do it

const keys = Object.keys(users);
for(let i=0; i<keys.length; i++){
  let user = keys[i]; //Alex or Asab
  let skills = users[user].skills;
  let skill_count = skills.length;
  console.log(`${user} has ${skill_count} skills.`)
}
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to be careful about which keys you are using, and how you use the value returned from Object.keys (which will be an array).

If you simply walk along each skills list for each user, then the time complexity of the algorithm will be O(m*n), where m represents the number of users, and n represents the number of skills per user.

If you want to make searching for a skill more efficient, you can keep the skills lists sorted and then use a searching algorithm to find the skill.

const users = {
    Alex: {
      email: 'alex@alex.com',
      skills: ['HTML', 'CSS', 'JavaScript'],
      age: 20,
      isLoggedIn: false,
      points: 30
    },
    Asab: {
      email: 'asab@asab.com',
      skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 
      'Node'],
      age: 25,
      isLoggedIn: false,
      points: 50
    },
}

const names = Object.keys(users)
console.log(names) // 'Alex', 'Asab'

const userSkills = users[names[0]].skills.join(', ')
console.log(userSkills) // 'HTML, CSS, JavaScript'

function getUserSkills(user) {
  return users[user].skills.join(', ')
}
console.log(getUserSkills('Alex')) // 'HTML, CSS, JavaScript'

function usersWithSkill(users, skill) {
  const userNames = Object.keys(users)
  const result = []
  
  for(let x = 0; x < userNames.length; x++) {
    const currentUser = users[userNames[x]]
    const currentUserSkills = currentUser.skills

    for(let y = 0; y < currentUserSkills.length; y++) {
      if(currentUserSkills[y] === skill) {
        result.push(userNames[x])
        break
      }
    }
  }
  
  return result.length ? result.join(', ') : 'Skill not found'
}

console.log(usersWithSkill(users, 'CSS')) // 'Alex, Asab'
console.log(usersWithSkill(users, 'MongoDB')) // 'Asab'
console.log(usersWithSkill(users, 'C')) // 'Skill not found'

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda