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.

1
  • 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. Commented Jul 26, 2019 at 16:06

2 Answers 2

1

Try this:

serverNamesEurope.forEach(elem => {
  console.log(elem.status)
})
Sign up to request clarification or add additional context in comments.

1 Comment

Just had a look at this solution and found this is more suited to my Project, thank you - marked as solution.
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

3 Comments

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.
@CameronShearer is a new contributor and while this answers the question doesn't provide the why/how. Teach him to fish IMO.
@alphapilgrim added the reference to .map which I believe would be helpful to @CameronShearer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.