There are a few ways to do this. Jay has already a good answer, but here's another one, with a few more pointers:
We start with your data:
const playersObj = {
'86wjIB7Xbz1tmwlTAAAB': {
rotation: 0.09999999999999964,
x: 579,
y: 579,
playerId: '86wjIB7Xbz1tmwlTAAAB' ,
},
'dWwtnOI8PryXJNDWAAAC': {
rotation: 0.09999999999999964,
x: 488,
y: 579,
playerId: 'dWwtnOI8PryXJNDWAAAC',
},
'GZPYpWdrzj9x0-SsAAAD': {
rotation: -0.09999999999999964,
x: 694,
y: 579,
playerId: 'GZPYpWdrzj9x0-SsAAAD',
},
};
Now, our first goal is to turn this object into a collection of objects - an array that we can operate on.
The easiest way is to grab the "keys" (property names) for the object:
const playerIds = Object.keys(playersObj); // gives us: ['86wjIB7Xbz1tmwlTAAAB', 'dWwtnOI8PryXJNDWAAAC', 'GZPYpWdrzj9x0-SsAAAD']
Now, you can loop those keys, playerIds, and return the actual objects. One simple way:
const players= playerIds.map(playerId => playersObj[playerId]);
This will give us the same data, but in an array, and we can operate on the array.
E.g. let's sort by x:
players.sort((a, b) => b.x - a.x) // sorts the collection
We can take it further, and get the first entry:
players.sort((a, b) => b.x - a.x)[0];
Finally, we only need it's playerId
property:
const leader = players.sort((a, b) => b.x - a.x)[0].playerId; // gives this: 'GZPYpWdrzj9x0-SsAAAD'
The whole thing as a runnable snippet:
const playersObj = {
'86wjIB7Xbz1tmwlTAAAB': {
rotation: 0.09999999999999964,
x: 579,
y: 579,
playerId: '86wjIB7Xbz1tmwlTAAAB',
},
'dWwtnOI8PryXJNDWAAAC': {
rotation: 0.09999999999999964,
x: 488,
y: 579,
playerId: 'dWwtnOI8PryXJNDWAAAC',
},
'GZPYpWdrzj9x0-SsAAAD': {
rotation: -0.09999999999999964,
x: 694,
y: 579,
playerId: 'GZPYpWdrzj9x0-SsAAAD',
},
};
// get the player keys
const playerIds = Object.keys(playersObj);
// turn players into a collection
const players = playerIds.map(playerId => playersObj[playerId]);
const leaderId = players.sort((a, b) => b.x - a.x)[0].playerId;
console.log(`Leader: ${leaderId}`);
Alternatively, as Felix suggests, you can skip the first few steps. You already have the playerId on each object, not just at the keys (as I originally thought), so you skip the `keys().map()' chain, like this:
const playersObj = {
'86wjIB7Xbz1tmwlTAAAB': {
rotation: 0.09999999999999964,
x: 579,
y: 579,
playerId: '86wjIB7Xbz1tmwlTAAAB',
},
'dWwtnOI8PryXJNDWAAAC': {
rotation: 0.09999999999999964,
x: 488,
y: 579,
playerId: 'dWwtnOI8PryXJNDWAAAC',
},
'GZPYpWdrzj9x0-SsAAAD': {
rotation: -0.09999999999999964,
x: 694,
y: 579,
playerId: 'GZPYpWdrzj9x0-SsAAAD',
},
};
// turn players into a collection
const players = Object.values(playersObj);
const leaderId = players.sort((a, b) => b.x - a.x)[0].playerId;
console.log(`Leader: ${leaderId}`);