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??
15 Replies
u mean, u want to list the users in an order of the most skilled one to the least skilled one?
Yesss
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 orderI am so confused 🤔
The forEach would work to loop over the object
Or no?
forEach or for loop both would work
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
Why the push?
@Sstephanyyy k i found it
It didn't work 😔
now it will
i took help from yt
tho I personally didn't understand the function part
I'll watch it again
I didn't understand it too 😅
search on yt..
how to sort array items
Thanks
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,
a - b
gives us the ascending order so
b - a will give the oppositeIf 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:
Then you have the for .. in
method of constructing the array:
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.
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