Using Knockout, I am trying to compute a nested array value and display it in the front-end. Please find my code below
View Model Code:
var viewModel = function () {
var self = this;
self.profile = ko.observableArray ([{
personal: [
{firstName: 'Captain', lastName: 'Flint'},
{firstName: 'Jhon', lastName: 'Silver'},
{firstName: 'Jack', lastName: 'Rackham'}
]
}]);
self.fullName = ko.computed (function () {
for (var i=0; i<self.profile()[0].personal.length; i++) {
self.profile()[0].personal[i].firstName+" "+ self.profile()[0].personal[i].lastName;
});
}
ko.applyBindings(new viewModel);
HTML
<table data-bind="foreach: profile">
<tbody data-bind="foreach: personal">
<tr>
<td data-bind="text: $index"></td>
<td data-bind="text: fullname"></td>
</tr>
</tbody>
</table>
Yes! this does not work. Tried Knockout documentation and many other tutorials unable find the best solution. Kindly help me with this issue. Thanks in advance.