I just started playing around with knockout and I have a question. Here’s part of the code:
function Task(data) {
var self = this;
self.name = ko.observable(data.name);
}
function ViewModel() {
self.taskArr = ko.observableArray([
// some default data
new Task({ name: "to-do 1"}),
new Task({ name: "to-do 2"}),
new Task({ name: "to-do 3"})
]);
Basically, I’m trying to display the contents of the object via console.log(). But when I use console.log(self.taskArr()); I get [Task, Task, Task] as a result.
Using self.taskArr()[0].name will only get the first result, not all of them.
In order to display the data of an observable array, you’ll have to use the ko.toJSON function, like so:
which will output:
More info here: http://knockoutjs.com/documentation/json-data.html