Need help doing a foreach inside a foreach

Hi,
I have a list of events I am returning. Each event is an object and each event object has an array of pricerange objects associated with it. Inside each of those price range objects is a price variable.

Event1
     PriceRange1
           Price: 14
     PriceRange2
           Price: 26
Event2
     PriceRange1
            Price: 10
     PriceRange2
           Price: 3

I am struggling to get all the prices into their own simple array. When I do a foreach I am getting confused with the returned arrays.

Please provide code related to your issue. Without it, it’s hard for us to help. You can refer to our guide on asking for help for more information. Thanks!

Bruh

let prices = []
Events.forEach(event=>{
  Event.forEach(priceRange=>{
    prices.push(priceRange.price)
  })
})

@JamesThomson Sorry about not posting code!
@lopu I tried doing that

let prices = []
                this.eventList.forEach(event=>{
                    event.forEach(priceranges=>{
                        prices.push(priceranges.price)
                    })
                })

but I am getting the error “TypeError: event.forEach is not a function”

Can you post an exact json example of your data structure? Not just Event1 PriceRange1 etc…

But [ [price1, price2] ]

Etc

0:
id: (...)
user_id: (...)
created_at: (...)
updated_at: (...)
isFavorited: (...)
shows: (...)
favorites: (...)
    priceranges: Array(2)
    0:
        id: (...)
        event_id: (...)
        price: "12.00"
        created_at: (...)
        updated_at: (...)
    1:
        id: (...)
        event_id: (...)
        price: "600.00"
        created_at: (...)
        updated_at: (...)

1:
id: (...)
user_id: (...)
created_at: (...)
updated_at: (...)
isFavorited: (...)
shows: (...)
favorites: (...)
priceranges: Array(2)
    0:
        id: (...)
        event_id: (...)
        price: (...)
        created_at: (...)
        updated_at: (...)
    1:
        id: (...)
        event_id: (...)
        price: (...)
        created_at: (...)
        updated_at: (...)

Change to

let prices = [] 
this.eventList.forEach(event=>{ 
  event.favourites.priceRanges.forEach(pricerange=>{ 
    prices.push(pricerange.price) 
  }) 
})

This is pretty simple stuff, learn to Google my dude, get your head around what data structures are

1 Like

Ahh, I see now that I missed the event.priceranges! That is why it wasnt working.
Thanks so much!

What data format is this anyway? Coffeescript or graphql or some shit?

I copied it from the console when I did a console.log()

What browser are you using?

I am using google Chrome