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: '[email protected]',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: '[email protected]',
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!
CodePudding user response:
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.`)
}
CodePudding user response:
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: '[email protected]',
skills: ['HTML', 'CSS', 'JavaScript'],
age: 20,
isLoggedIn: false,
points: 30
},
Asab: {
email: '[email protected]',
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'