Home > Back-end >  how to access objects in a object
how to access objects in a object

Time:06-25

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 -----

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']
    }
}

CodePudding user response:

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);

CodePudding user response:

Array.prototype.reduce() can be used for running through all objects.

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 user = Object.keys(users).reduce((prev, name) => {
  return (users[name].skills.length > users[prev].skills.length) ? name : prev;
});


console.log(user);

CodePudding user response:

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",
    ],
  },
};
// method 1, step by step

const userSkillMap = Object.keys(users).map((eachUser) => {
  const skillLength = users[eachUser]["skills"].length;
  return {
    [eachUser]: skillLength,
  };
});

const sortedUsers = userSkillMap.sort((prev, current) => {
  const prevUserName = Object.keys(prev)[0];
  const currentUserName = Object.keys(current)[0];
  return current[currentUserName] - prev[prevUserName];
});

const userWithMostSkills = sortedUsers[0];

console.log("map is", userSkillMap);
console.log("sortedUsers", sortedUsers);

console.log("userWithMostSkills is", userWithMostSkills);

// if you wanna chain everything, method 2

const userWithMostSkillsCombine = Object.keys(users)
  .map((eachUser) => {
    const skillLength = users[eachUser]["skills"].length;
    return {
      [eachUser]: skillLength,
    };
  })
  .sort((prev, current) => {
    const prevUserName = Object.keys(prev)[0];
    const currentUserName = Object.keys(current)[0];
    return current[currentUserName] - prev[prevUserName];
  })[0];

console.log("userWithMostSkillsCombine is", userWithMostSkillsCombine);

Hope it works for you, let me know!

  • Related