Manipulate OBJECTS

In this object of users, I want to find to the person who has manu skills in the user object to the person who has less skills. Could you explain in detail how could I make this??
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
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel: {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John: {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas: {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul: {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}

}
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
},
Brook: {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel: {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John: {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas: {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul: {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}

}
15 Replies
glutonium
glutonium•16mo ago
u mean, u want to list the users in an order of the most skilled one to the least skilled one?
Sste
Sste•16mo ago
Yesss
glutonium
glutonium•16mo ago
hmmm i don't have good amount of practice with objects, but what if u rather then making the const user an object of it's own, make is an array const users = [] then loop over the array .. if this works this should solve the 1st phase of the problem.. I'm still not sure to u can sort the items out in the descending order
Sste
Sste•16mo ago
I am so confused 🤔 The forEach would work to loop over the object Or no?
glutonium
glutonium•16mo ago
forEach or for loop both would work
let skill = []
users.forEach((user)=>{
skill.push(user.skill.length);
});
let skill = []
users.forEach((user)=>{
skill.push(user.skill.length);
});
perhaps like this.. I'm not sure if this will even work but if it does, it'll just store the skill numbers for each user
Sste
Sste•16mo ago
Why the push?
glutonium
glutonium•16mo ago
@Sstephanyyy k i found it
Sste
Sste•16mo ago
It didn't work 😔
glutonium
glutonium•16mo ago
now it will
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
},
Brook = {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel = {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John = {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas = {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul = {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}

]


users.sort((a,b)=>{
return a.skills.length - b.skills.length;
})

users.reverse();

console.log(users);
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
},
Brook = {
email: 'daniel@daniel.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux'],
age: 30,
isLoggedIn: true,
points: 50
},
Daniel = {
email: 'daniel@alex.com',
skills: ['HTML', 'CSS', 'JavaScript', 'Python'],
age: 20,
isLoggedIn: false,
points: 40
},
John = {
email: 'john@john.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React', 'Redux', 'Node.js'],
age: 20,
isLoggedIn: true,
points: 50
},
Thomas = {
email: 'thomas@thomas.com',
skills: ['HTML', 'CSS', 'JavaScript', 'React'],
age: 20,
isLoggedIn: false,
points: 40
},
Paul = {
email: 'paul@paul.com',
skills: ['HTML', 'CSS', 'JavaScript', 'MongoDB', 'Express', 'React', 'Node'],
age: 20,
isLoggedIn: false,
points: 40
}

]


users.sort((a,b)=>{
return a.skills.length - b.skills.length;
})

users.reverse();

console.log(users);
i took help from yt tho I personally didn't understand the function part I'll watch it again
Sste
Sste•16mo ago
I didn't understand it too 😅
glutonium
glutonium•16mo ago
search on yt.. how to sort array items
Sste
Sste•16mo ago
Thanks
glutonium
glutonium•16mo ago
welcome @Sstephanyyy btw..here u don't need the .reverse() method here. insted u can swap the a and b in the return code.. so it'll look like,
return b.skills.length - a.skills.length
return b.skills.length - a.skills.length
a - b gives us the ascending order so b - a will give the opposite
Queequeg
Queequeg•16mo ago
If you can not change your users data object - you can always use Object.keys , Object.values, or for..in to get access to the keys and values as an array. * Object.keys returns an array containing all of the object property names as strings: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/keys * Object.values returns all of the values stored in the keys of the object: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/values * for .. in loops would give you access to the keys as well, but in a for loop: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...in So to create an array of users from your object you might use: const userList = Object.values() But you will lose the user name, because that is stored in the key. Instead you might use map with key like so: const userList = Object.keys().map((key) => ({ ...users[key], name: key })); Note that I am also using Array and Object "Destructuring" (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Destructuring_assignment) to make this short... but it can also happen more clearly like so:
const userList = Object.keys().map((key) => {
const user = users[key];
user.name = key;
return user;
});
const userList = Object.keys().map((key) => {
const user = users[key];
user.name = key;
return user;
});
Then you have the for .. in method of constructing the array:
const userList = [];
for (let name in users) {
const user = users[name];
user.name = name;
userList.push(user);
}
const userList = [];
for (let name in users) {
const user = users[name];
user.name = name;
userList.push(user);
}
After all of this, you can easily use the sort method that "Boob 2.0" suggested.
Object.keys() - JavaScript | MDN
The Object.keys() static method returns an array of a given object's own enumerable string-keyed property names.
Object.values() - JavaScript | MDN
The Object.values() static method returns an array of a given object's own enumerable string-keyed property values.
for...in - JavaScript | MDN
The for...in statement iterates over all enumerable string properties of an object (ignoring properties keyed by symbols), including inherited enumerable properties.
Destructuring assignment - JavaScript | MDN
The destructuring assignment syntax is a JavaScript expression that makes it possible to unpack values from arrays, or properties from objects, into distinct variables.
BERSERK
BERSERK•16mo ago
hi you can loop over the users and go inside the skills property and return the length of the array then make a condition that if the length >others length then make the user in the first index and so on
Want results from more Discord servers?
Add your server
More Posts