0

I'm working with node.js to try and get some values of a JSON using an api, I'm trying to loop through and get the status values in the JSON how would I go about doing this. The JSON is shown below

var serverNamesEurope = [
                        { name: result.Chars.Servers[0].Server[19].Name, status: result.Chars.Servers[0].Server[19].Usage, return: ""},
                        { name: result.Chars.Servers[0].Server[21].Name, status: result.Chars.Servers[0].Server[21].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[9].Name, status: result.Chars.Servers[0].Server[9].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[10].Name, status: result.Chars.Servers[0].Server[10].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[7].Name, status: result.Chars.Servers[0].Server[7].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[14].Name, status: result.Chars.Servers[0].Server[14].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[12].Name, status: result.Chars.Servers[0].Server[12].Usage, return: "" }
                    ]

What I want to do is be able to loop through this JSON and then return the status on a seperate line in the console for now.

Cameron Shearer
  • 142
  • 1
  • 10
  • 1
    Hi Cameron, I'd like to suggest that you add what you've tried to the question. Many people are willing to help but would like to see what you've attempted. That said, [here's a post on looping through arrays](https://stackoverflow.com/questions/3010840/loop-through-an-array-in-javascript). – alphapilgrim Jul 26 '19 at 16:06

2 Answers2

1

You could do something like this:

var serverNamesEurope = [
                        { name: result.Chars.Servers[0].Server[19].Name, status: result.Chars.Servers[0].Server[19].Usage, return: ""},
                        { name: result.Chars.Servers[0].Server[21].Name, status: result.Chars.Servers[0].Server[21].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[9].Name, status: result.Chars.Servers[0].Server[9].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[10].Name, status: result.Chars.Servers[0].Server[10].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[7].Name, status: result.Chars.Servers[0].Server[7].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[14].Name, status: result.Chars.Servers[0].Server[14].Usage, return: "" },
                        { name: result.Chars.Servers[0].Server[12].Name, status: result.Chars.Servers[0].Server[12].Usage, return: "" }
                    ]

console.log(serverNamesEurope.map(record => record.status).join("\n"))

More on map here and how it works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map

mehulmpt
  • 15,861
  • 12
  • 48
  • 88
  • Thanks alot! That was very fast and I'm rather new to this, looks very simple now... When I can accept the answer I will however it's on an interval. – Cameron Shearer Jul 26 '19 at 16:05
  • @CameronShearer is a new contributor and while this answers the question doesn't provide the why/how. Teach him to fish IMO. – alphapilgrim Jul 26 '19 at 16:12
  • @alphapilgrim added the reference to `.map` which I believe would be helpful to @CameronShearer :) – mehulmpt Jul 26 '19 at 16:17
1

Try this:

serverNamesEurope.forEach(elem => {
  console.log(elem.status)
})
Hamza
  • 469
  • 3
  • 9