Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to access objects in a object

I couldn’t find answer for this anywhere this is my last hope .
(stuck in learning!!!)

Find user with most skills .
I have no clue how I can use for loop here !!

I tried —–

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

x=[]
users.getfuction = function(){
  x.push(users.Alex.skills.length)
  x.push(users.Asab.skills.length)
  x.push(users.Brook.skills.length)
  x.push(users.Daniel.skills.length)
  x.push(users.John.skills.length)
  x.push(users.Paul.skills.length)
  x.push(users.Thomas.skills.length)
  max=x[0]
    for(i=1;i<x.length;i++){
      if(x[i]>max){
        max=x[i]
      }
    }
  return z
}
console.log(users.getfuction())
//I dont want to keep pushing the name one by one!!
const users = {
    Alex: {
      skills: ['HTML', 'CSS', 'JavaScript']
    },
    Asab: {
      skills: ['HTML', 'CSS', 'JavaScript', 'Redux', 'MongoDB', 'Express', 'React', 'Node']
    },
    Brook: {
      skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux']
    },
    Daniel: {
      skills: ['HTML', 'CSS', 'JavaScript', 'Python']
    },
    John: {
      skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js']
    },
    Thomas: {
      skills: ['HTML', 'CSS', 'JavaScript', 'React']
    },
    Paul: {
      skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node']
    }
}

>Solution :

Object.entries

returns an array of a given object’s own enumerable string-keyed property [key, value] pairs.

The pseudo-code of this is

loop through each user:
  if current_user's skill count is greater than the record:
    set current_user as the best user
    save current_user's skill count for comparison

print out best user

[name, {skills}] is a thing called destructuring. This is the same as saying:

for (const entry of Object.entries(users)) {
  const name = entry[0];
  const skills = entry[1].skills;
  if (skills.length > bestUserSkills) {
    bestUserName = name;
    bestUserSkills = skills.length;
  }
}
const users = {Alex:{skills:['HTML','CSS','JavaScript']},Asab:{skills:['HTML','CSS','JavaScript','Redux','MongoDB','Express','React','Node']},Brook:{skills:['HTML','CSS','JavaScript','React','Redux']},Daniel:{skills:['HTML','CSS','JavaScript','Python']},John:{skills:['HTML','CSS','JavaScript','React','Redux','Node.js']},Thomas:{skills:['HTML','CSS','JavaScript','React']},Paul:{skills:['HTML','CSS','JavaScript','MongoDB','Express','React','Node']}};

let bestUserName = "";
let bestUserSkills = -1;

for (const [name, {skills}] of Object.entries(users)) {
  if (skills.length > bestUserSkills) {
    bestUserName = name;
    bestUserSkills = skills.length;
  }
}

console.log(bestUserName);
const bestUser = users[bestUserName];
console.log(bestUser);
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading