0

Have an array nested inside another array.

const data = [
    id: 1,
    plan_name: foo,
    description: foo bar,
    test: [{
           id: 44,
           activity_name: bar,
           comment: var
           },
           {
           id: 45,
           activity_name: var,
           comment: bar
           }],
    userId: 3
];

Printing data in a view template using:

{{#each data}}
    <p>{{plan_name}}</p>
    <p>{{test}}</p>
{{/each}}

The output is:

p1
[object Object],[object Object]
p2
[object Object],[object Object],[object Object]
p3
[object Object],[object Object]

How can I access the objects in the nested array using handlebars so that for every instance in the data array all items in the test array are printed?

3
  • Test is an array containing a number of objects. Edited to make it more clear Commented Jan 19, 2019 at 9:42
  • Ok, in that case try the code I suggested below. Commented Jan 19, 2019 at 9:49
  • This code example should not even work, it's not a correct array syntax Commented Oct 4, 2023 at 14:22

2 Answers 2

1

Try this:

{{#each data}}
    <p>{{plan_name}}</p>>
    {{#each test}}
        {{activity_name}}
        {{comment}}
    {{/each}}
{{/each}}
Sign up to request clarification or add additional context in comments.

1 Comment

That did the trick, this was actually what i tried originally with another result, must have been some magic along the way.
1

You can use the #with handlebar to access the object

{{#each data}}
    <p>{{plan_name}}</p>>
    {{#with test}}
        {{activity_name}}
        {{comment}}
    {{/with}}
{{/each}}

3 Comments

Still getting the same output for some reason, feels like it should work though
Getting this output now instead p1 > p3 > p3 >
Is ‘test’ meant to be an object? If so it should be enclosed within curly brackets {} instead of []. If it’s an array or objects then each object should be within the curly bracket like test=[{id:1, ..}, {id:2,..}]. If it’s an object, then the suggestion above should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.