You can write a special binding handler for this, but I would prefer to leave the templates free from such ugly details.
I would recommend writing ko.computed :
function Model() { this.data = ko.observableArray(["Sample", 100, "Sample 2", 200]) this.items = ko.computed(function() { var value = []; var data = this.data(); for (var i=0; i<data.length; i+=2) { value.push({ key: data[i], value: data[i+1]); } return value; }, this); } var model = new Model();
Now you can iterate over items in your template and access key and value . Updating the original data array will automatically restore the calculated items value.
The template code will look like this:
<ul data-bind="foreach: items"> <li><span data-bind="text: key"></span>: <span data-bind="text: value"></span></li> </ul>
source share