Search code examples
knockout.jsko.observablearraycomputed-observable

How to create a computed observable array in Knockout


I would like to know how to create a computed observable array.

In my view model, I have 2 observable arrays, and I would like to have a computed observable array that is simply both arrays combined.

function ViewModel() {
    var self = this;
    self.listA= ko.observableArray([]);
    self.listB = ko.observableArray([]);
    self.masterList= //combine both list A and B

Solution

  • This will combine the two arrays and return the combined list. However, it is not a computed observable array (don't know if that is even possible), but a regular computed observable.

    self.masterList = ko.computed(function() {
        return this.listA().concat(this.listB());
    }, this);