DEV Community

Cover image for Treating a JSON object as an array for iterations
technicallyty
technicallyty

Posted on

Treating a JSON object as an array for iterations

Hello there!

I came across a problem where an API was returning data filtered by date. Each JSON entry had a couple fields (activeUsers: 300, emailsSent: 500.. stuff like that).

I wanted to create a bar graph that showed the lifetime totals. Since you can't grab a JSON object and just go jsonThing[0] I had to find a way to index through it properly so I could sum up the values.

yourJsonObject[Object.keys(yourJsonObject)[index]]

Thats it! Now you can treat your JSON object as an array.

I'm mainly making this post so I can collect ideas/problems I've had previously in case they come up again. If you're reading this, I hope you may find some value. Have a nice day!

Top comments (0)