Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to access sub objects in a loop #29

Open
vestaxpdx opened this issue Jan 28, 2017 · 3 comments
Open

How to access sub objects in a loop #29

vestaxpdx opened this issue Jan 28, 2017 · 3 comments

Comments

@vestaxpdx
Copy link

vestaxpdx commented Jan 28, 2017

Hi

How can I loop through the following object properly.

Here is my JSON before I parse it with JSON.parse();

[{
  "id": "1",
  "title": "Article Title 1",
  "videos": [
        {"title":"Video Title 1"},
        {"title":"Video Title 2"},
        {"title":"Video Title 3"}
   ]
},
{
  "id": "2",
  "title": "Article Title 2",
  "videos": [
        {"title":"Video Title 1"},
        {"title":"Video Title 2"},
        {"title":"Video Title 3"}
   ]
}]

I have tried the following but it spits out loads of video titles. It is not looping properly and I can't figure out how to access to sub object loop right. Is it even possible?

{{#each articles}}
    <h2>{{this.title}}</h2>
    {{#each this.videos}}
         <h3>{{this.videos.title}}</h3>
    {{/each}}
{{/each}}

It kind of just ends up spitting out every singe sub video in one.

Would really appreciate anyone's help on this.

Cheers

@vestaxpdx
Copy link
Author

Any ideas would really appreciate your help @nolimits4web

@asessa
Copy link

asessa commented Feb 1, 2017

@vestaxpdx check this jsfiddle:

https://jsfiddle.net/wcwh3p4r/

@vestaxpdx
Copy link
Author

Ah thank you so much.

Somehow my videos object was still as a JSON string. You showing me it should still work caused me to investigate further and spot the obvious error! So thank you!

Interesting to know you don't have to use 'this.'. From the documentation I always thought once in the loop you should be using this.property to access. Perhaps it's just for readability.

Many thanks again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants