How to access Object that's inside an Array using instantsearch.widgets.hits widget

json-array-then-object

Hi, I’m trying to figure out how to access ‘groups’ from instantsearch.widgets.hits()

This doesn’t work hit.groups.category_slug

thank you in advance,
Kurt

You have an array of objects at this point, so either a forEach() loop would work, or hit.groups[0].category_slug should work

hi Michael, thank you very much for your help. One point for anyone else who reads this post - I had tried
this ${components.Highlight({ attribute: ‘groups[0].category_name’, hit })} but it didn’t work. But this did work ${hit.groups[0].category_name}

It’s fine because I don’t really need it highlighted in search. I do have some instances where there are multiple items so I’ll try the forEach() loop for that. Have a great day!

Hi @tw2113 I was trying the forEach() but a little confused as where to put it. I’m using - return html `` to display my content. How do I put JS in there?

Same person, I just happen to have 2 different accounts.

I would check out javascript - Template Literals with a ForEach in it - Stack Overflow

It discusses forEach() vs map() for which both would be valid. Just need to get an array looped over one way or another.

1 Like

thank you! will check that out