DEV Community

Discussion on: Javascript - Object is loopable too??

Collapse
chadnaz profile image
Chad Naz

I deal with this all the time with 3rd party APIs. Many of the ones I use return an object of objects, instead of an array of objects like most normal APIs would do.

Annoying, but not a huge deal when you can just loop a for in/of and push the objects to an array - then perform normal array methods on the data.

This post is relevant for anyone working with older/non standard/shitty APIs

Collapse
alserembani profile image
Atif Aiman Author • Edited on

Some API, they prefer putting keys as something that represent the object, such as this

{
  "Atif": {
    age: 11,
    pref: true,
  },
  "Aiman": {
    age: 21,
    pref: false,
  },
}
Enter fullscreen mode Exit fullscreen mode

This way, backend can do a better indexing for more data processing. Though I am somewhere 85-92% agree of what you said, some of them had their reason. Though supposely when dealing with API to the frontend, I do favours array of objects instead.

Cheers 😎

Collapse
chadnaz profile image
Chad Naz

Good point. The only apis that return object of objects are old, outdated, and or not maintained in my experience. Which backs up your point - old technology might have needed faster indexing on the backend doing this but now a days just gimme the damn array. 😁