I want to assign a array value into a variable in knockoutjs. I can iterate over a array by for loop. My code is
<!-- ko foreach: {data: friends, as: 'friend'} -->
<span data-bind="text:friend"></span>
<!-- /ko -->
<script type="text/javascript">
var viewModel = {
firstname: ko.observable("X"),
lastname: ko.observable("Y"),
friends: ko.observableArray(["A", "B"]),
books: ko.observableArray(["Book1", "Book2"]),
};
viewModel.fullname = ko.dependentObservable(function () {
return this.firstname() + " " + this.lastname();
}, viewModel);
ko.applyBindings(viewModel);
</script>
But I want book's first element and then iterate over friends. Like
<!-- ko {books()[0], as : 'book'} -->
<!-- ko foreach: {data: friends, as: 'friend'} -->
<span data-bind="text:friend"></span>
<span data-bind="text:book"></span>
<!-- /ko -->
<!-- /ko -->
I know my approach is wrong. But I cann't figure out how can i get first book's element and then iterate through friends.