Reputation: 13
So this is an output object that I get. this is when you output. sampresponse.players with samp-query npm
[
{ id: 0, name: 'djandar', score: 127, ping: 82 },
{ id: 1, name: 'Nememuruhilebey', score: 69, ping: 98 },
{ id: 2, name: 'illuminateForces', score: 13219, ping: 108 },
{ id: 3, name: 'floridian.', score: 200, ping: 168 },
{ id: 4, name: 'afdan', score: 2583, ping: 67 },
{ id: 5, name: 'kaxni', score: 3210, ping: 93 },
{ id: 6, name: 'Sahin_K', score: 415, ping: 92 },
{ id: 7, name: 'Frank1061', score: 251, ping: 77 },
{ id: 8, name: 'Alphatester', score: 4068, ping: 311 },
{ id: 9, name: 'Vyp3r', score: 1893, ping: 98 },
{ id: 10, name: 'SerbieNikLalbanie', score: 1, ping: 70 },
{ id: 11, name: 'skkkrt', score: 880, ping: 114 },
{ id: 12, name: 'SikikM4', score: 0, ping: 99 },
{ id: 14, name: 'metowill', score: 1398, ping: 88 },
{ id: 16, name: 'Bob_Alonzo', score: 206, ping: 96 },
{ id: 18, name: 'dzandar', score: 54, ping: 81 },
{ id: 20, name: 'Clay', score: 0, ping: 515 },
{ id: 22, name: 'Sabitto', score: 568, ping: 114 },
{ id: 23, name: 'Leidys', score: 3832, ping: 97 },
{ id: 25, name: 'killergod', score: 4688, ping: 95 },
{ id: 26, name: '.dose', score: 11058, ping: 86 },
{ id: 27, name: 'Trail', score: 4024, ping: 70 },
{ id: 28, name: 'Lil_Alperr', score: 6784, ping: 80 },
{ id: 31, name: 'Aliisthebest', score: 3, ping: 301 },
{ id: 32, name: 'memurbeey', score: 150, ping: 96 },
{ id: 33, name: 'Sagem', score: 5294, ping: 45 },
{ id: 35, name: 'KoBi1', score: 3156, ping: 133 },
{ id: 36, name: 'realturkoncel', score: 10460, ping: 86 }
]
The number of lines depends on how many players are online on the samp server. using samp-query npm So I need my output to be like this. For example, this would be first line. as id 0.
0 djandar 127 82
I tried to do it like this:
sampresponse.players[0].name
but that only shows the name in the first line I need it to show all names. I also tried this.
sampresponse.players.name
But that comes as undefined. I have one idea but I don't know how to execute it. So when you go like this
sampresponse.players.length
the out put is 36 as the number of last id. So my idea is to loop throw all number from 0 to the number output by sampresponse.players.length which is the last number. Something like this 0 - sampresponse.players.length But I don't know how to loop throw all the numbers and show all the names, scores and pings, so I was hoping some of you can help me.
Upvotes: 0
Views: 90
Reputation: 13
Yes, just remap to whatever you want. It's the same idea again - you get each item in the map callback and you can extract any property you want, produce any string you want and finally you'd get a new array based on the transformation defined in map. So if you do sampresponse.players.map(x => x.name + " " + x.id) You'd get the name and ID separated by a space. Or you can do whatever format suits you. – VLAZ
That solved my problem thanks for all your help.
Upvotes: 0
Reputation: 2843
You can use Array.map()
to transform the values of an array.
sampresponse.players.map(player => [
player.id,
player.name,
player.score,
player.ping
]);
This will result in an output looking like this:
[
[0, "djandar", 127, 82],
[1, "Nememuruhilebey", 69, 89],
... //all the other players
];
Is this the desired result?
Upvotes: 0
Reputation: 7414
Just iterate over the array:
const data = [
{ id: 0, name: 'djandar', score: 127, ping: 82 },
{ id: 1, name: 'Nememuruhilebey', score: 69, ping: 98 },
{ id: 2, name: 'illuminateForces', score: 13219, ping: 108 },
{ id: 3, name: 'floridian.', score: 200, ping: 168 },
{ id: 4, name: 'afdan', score: 2583, ping: 67 },
{ id: 5, name: 'kaxni', score: 3210, ping: 93 },
{ id: 6, name: 'Sahin_K', score: 415, ping: 92 },
{ id: 7, name: 'Frank1061', score: 251, ping: 77 },
{ id: 8, name: 'Alphatester', score: 4068, ping: 311 },
{ id: 9, name: 'Vyp3r', score: 1893, ping: 98 },
{ id: 10, name: 'SerbieNikLalbanie', score: 1, ping: 70 },
{ id: 11, name: 'skkkrt', score: 880, ping: 114 },
{ id: 12, name: 'SikikM4', score: 0, ping: 99 },
{ id: 14, name: 'metowill', score: 1398, ping: 88 },
{ id: 16, name: 'Bob_Alonzo', score: 206, ping: 96 },
{ id: 18, name: 'dzandar', score: 54, ping: 81 },
{ id: 20, name: 'Clay', score: 0, ping: 515 },
{ id: 22, name: 'Sabitto', score: 568, ping: 114 },
{ id: 23, name: 'Leidys', score: 3832, ping: 97 },
{ id: 25, name: 'killergod', score: 4688, ping: 95 },
{ id: 26, name: '.dose', score: 11058, ping: 86 },
{ id: 27, name: 'Trail', score: 4024, ping: 70 },
{ id: 28, name: 'Lil_Alperr', score: 6784, ping: 80 },
{ id: 31, name: 'Aliisthebest', score: 3, ping: 301 },
{ id: 32, name: 'memurbeey', score: 150, ping: 96 },
{ id: 33, name: 'Sagem', score: 5294, ping: 45 },
{ id: 35, name: 'KoBi1', score: 3156, ping: 133 },
{ id: 36, name: 'realturkoncel', score: 10460, ping: 86 }
]
data.forEach(item => {
console.log(item.id, item.name, item.score, item.ping);
})
Upvotes: 1