whatever
whatever

Reputation: 157

Iterating over the nested Array in APi

I am trying to learn ember through building stuff, but now I am kind of stuck with looping through the nested array, Api is mine, so you can visit to see the data, anyway, the thing I am trying to accomplish is this, I want to display the category property, in this nested array I would like some help if possible.

import Route from '@ember/routing/route';
export default class BooksRoute extends Route {
  async model(){
    let response = await fetch('https://json-api-smaiil.herokuapp.com/books');
    let parsed = await response.json()
    console.log(parsed)
    return parsed.map((book) =>{
      let {categories} = book
    })
  }
}

Upvotes: 1

Views: 61

Answers (1)

Jon Kolman
Jon Kolman

Reputation: 26

What you want to do is wrap the parsed in Object.keys() then each key will be the category. Like this:

Object.keys(parsed).map(category => {
    console.log(category)
    let book = parsed[category]
})

Upvotes: 1

Related Questions