DEV Community

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

Posted on

1 1

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)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay