我有一个ViewModel
带有可观察数组的 knockout.js:
function ItemsViewModel() {
this.data = ko.observableArray([
new Item(1, "One description"),
new Item(2, "Two description"),
new Item(3, "Three description"),
// ... etc
]);
}
该项目如下所示:
function Item(id, name) {
this.id = ko.observable(id);
this.name = ko.observable(name);
};
基于我在我创建的可观察数组,我ViewModel
想创建第二个计算数组,如下所示:
function ItemsViewModel() {
this.data = ko.observableArray([
new Item(1, "One description"),
new Item(2, "Two description"),
new Item(3, "Three description"),
// ... etc
]);
this.computedData = // here I want to create a computed array based on the values
// of this.data
}
我似乎无法在 knockout.js 文档的任何地方找到如何创建此计算数组的示例。您能否给我一个示例,说明如何将第一个数组转换为以下形式的计算数组:
this.computedData = [
{ dataItem: data[0].name + ' ' + data[0].id },
{ dataItem: data[1].name + ' ' + data[1].id },
// ... etc.
]