1

如何在 kncouout js 中获取 ObservableArray 的所有属性让我们考虑淘汰 js 中的 ObservableArray

var repotviewmodel = function()
{
 this.types = ko.observableArray([
      { Id: '', Type: 'All' }, 
      { Id: '0', Type: 'Big file' }, 
      { Id: '1', Type: 'File' }, 
      { Id: '2', Type: 'Business' }, 
      { Id: '3', Type: 'Social Media'}
 ]);

所以我想要像这样的值IdType在一个数组中这是一个硬编码的数组,但在我的情况下,数组从服务器端数据返回所以我怎样才能获得数组的所有属性,这样结果就会像

    this.Items =  ko.observableArray('Id','Type','firstName'......)

        };
4

1 回答 1

3

您可以遍历对象的属性:

var self = this;
var myObject = self.types()[0];
for (var property in myObject) {
    if (myObject.hasOwnProperty(property)) {
        self.items.push(property);
    }
}

这是一个例子:http: //jsfiddle.net/8Y9ru/

于 2013-07-26T08:22:50.397 回答