1

I have an array of arrays that contain objects, and im looking for a way to loop over the inner arrays of the array, something like this:

<div data-bind="foreach: questions">
    <div data-bind="foreach: subArray of questions">
        <span data-bind="text: Title"></span>
        <span data-bind="text: Answer"></span>
    </div>
</div>

How can i access the inner arrays of the questions observableArray so i can loop over the elements?

Fiddle example

thanks in advance for any help!

2 Answers 2

3

I have updated your fiddle . To use inner loop knockout has provided various properties like $data, to use outer loop $parent.

<div class="answers" data-bind="foreach: questions">
    <div data-bind="foreach: $data ">
      <p data-bind="text: Title"></p>
      <p data-bind="text: Answer"></p>  
    </div>  
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help
2

It's all in the docs, you just have to access inner arrays with $data: http://knockoutjs.com/documentation/foreach-binding.html

<div class="answers" data-bind="foreach: questions">
  <div data-bind="foreach: $data ">
    <p data-bind="text: Title"></p>
    <p data-bind="text: Answer"></p>  
  </div>
</div>

Comments

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.