0

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.

CroatiaGM
  • 5
  • 2
  • `sampresponse.players.map(x => x.name)` – VLAZ Mar 04 '20 at 15:03
  • 1
    Does this answer your question? [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) – VLAZ Mar 04 '20 at 15:04
  • sampresponse.players.map(x => x.name) This displays all the names but is there a way to remove , and display one name per line something like this name1 (ID) - score - ping new line name2 (ID) - score - ping and like that for all the players. – CroatiaGM Mar 04 '20 at 15:17
  • 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 Mar 04 '20 at 15:23
  • Yeah, that worked that I got format that I wanted. Thanks. One last thing I don't know is how to get one player per line. Now they are just one by one separated by , Is there a way to get then one player per line. – CroatiaGM Mar 04 '20 at 15:32
  • I got a new line with \n and the only thing left is that I don't know is how to remove , – CroatiaGM Mar 04 '20 at 15:35
  • You have an array. If you need the items separated by a newline, then just do `arr.join("\n")` – VLAZ Mar 04 '20 at 15:45
  • Thanks for all the help I got it working like this. first i difined that map as player list then playerlist.toString().replace(/,/g, "") with that i removed the , and new line with /n, – CroatiaGM Mar 04 '20 at 15:50

3 Answers3

1

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);
})
Horatiu Jeflea
  • 7,256
  • 6
  • 38
  • 67
0

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?

VLAZ
  • 26,331
  • 9
  • 49
  • 67
Luïs
  • 2,671
  • 1
  • 20
  • 33
0

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.

CroatiaGM
  • 5
  • 2