capiono
capiono

Reputation: 2997

loading observableArray

I'm using Knockout in Typescript, I want to load data returned from db into an observableArray.

I tried the below code, but I got an exception:

Object doesn't support property or method 'map'

in constructor:

this.boxes = ko.observableArray<Box>(data[0].box || []).map(e => new Box(
                e.index,
                e.title,
                e.value,
                e.category
            ));


this.boxes = ko.observableArray<Box>([]);

Upvotes: 0

Views: 57

Answers (1)

JohnnyHK
JohnnyHK

Reputation: 311865

map is a method on a normal JavaScript array, not an ObservableArray, so you need to perform the mapping on the array from the response data before passing it to ko.observableArray<Box>():

this.boxes = ko.observableArray<Box>((data[0].box || []).map(e => new Box(
            e.index,
            e.title,
            e.value,
            e.category
        )));

Upvotes: 1

Related Questions