-1
debug: {ping: true, query: false, srv: false, querymismatch: false, ipinsrv: false, …}

eula_blocked: false
ip: "45.146.253.38"
online: true
players: {online: 0, max: 20}
port: 2018
version: "1.20.1"

I want to filter out online: 0and max: 20using JavaScript. (The array is stored in a variable and comes from an API).

EDIT: I found a way to filter the values out, but it somehow only works if I manually input my data, and not if I use the data from the variable of the API response:

const data = {
    players:{online: 0,max: 20}
}

console.log("")
console.log("players online:", data.players.online)
console.log("players max:   ", data.players.max)

This works with the const data but not with the actual variable I want to work with, although it has the same structure. Why is that?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
de_trixa
  • 1
  • 1
  • By filter out, you mean remove it from the result or only online and max would be the result? Could you show the expected output? – dork Jul 27 '23 at 03:40
  • are use sure you resolved the data with `await` or `then()` ? https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Alexander Nenashev Jul 27 '23 at 06:53

2 Answers2

1

The Array.filter is going to return the elements that follows an logic, for example [1,2,3].filter(num => num < 2) will return [1] so If you want a new array with the objects that have the properties online = 0 and max = 20 you should go for:

array.filter(item => {
  return item.players.online === 0 && item.players.max === 20;
}
  • Should be `item.players.online` – Barmar Jul 27 '23 at 03:47
  • Yes, sorry. Didn't saw where these properties were writen so I answered thinking in a general object. – Gabriel Ayres Jul 27 '23 at 04:01
  • You can check the edit I made for my question if you want, maybe you have an idea on how to fix my trashy code.... – de_trixa Jul 27 '23 at 04:02
  • I didn't understand yet what you want. Do you want an array with all the objects that have online = 0 and max = 20? Also, could you share more code, because since it works with manual input and doesn't work with the API return, it seems to be an problem with your fetch. – Gabriel Ayres Jul 27 '23 at 04:16
0
const responseData = [
  {
    debug: { ping: true, query: false, srv: false, querymismatch: false, ipinsrv: false },
    eula_blocked: false,
    ip: "45.146.253.38",
    online: true,
    players: { online: 0, max: 20 },
    port: 2018,
    version: "1.20.1",
  },
  // Add more objects here if there are multiple data points
  // {
  //   ...
  // },
];

// Extracting the 'online' and 'max' values
const playersInfo = responseData.map((data) => {
  const { online, max } = data.players;
  return { online, max };
});

console.log(playersInfo);

Map function iterates over each element in the responseData array and extracts the online and max values from the players object of each element. It then creates a new array called playersInfo containing objects with the extracted online and max values for each data point.

Isha Padalia
  • 877
  • 7
  • 24