I have a nested foreach loop in knockout.js and I want to access a property from the current object in the outer loop inside the inner loop. How would I do this?
<!-- ko foreach: graduationDateRows -->
<tr>
<td class="center" data-bind="text: CalendarYear"></td>
<!-- ko foreach: $root.graduationDatesHeaders -->
<td class="center" data-bind="text: /* !here! */"></td>
<td></td>
<!-- /ko -->
</tr>
<!-- /ko -->
To access a property in the outer loop from the current object in the inner loop you can use
$parent.property_name
.E.g.:
Sample object of
salesInfo
array:You can use
$parent
to access one scope level up. So, from your inner loop you can useparent
to access the current item being looped on in yourgraduationDateRows
You can even loop through completely unrelated arrays using
$parent
andas
aliasing inforeach
binding.Consider the following example: