Chitta
Chitta

Reputation: 185

How to get the all properties of an ObservableArray in knockout js

How can I get all the properties of an ObservableArray in kncouout js let us consider a ObservableArray in knockout js

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'}
 ]);

So I want the values like Id, Type in an array this a hard coded array but in my case the array returns from the server side data So how can I get all the properties of the array so that result will be like

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

        };

Upvotes: 1

Views: 1295

Answers (1)

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You can iterate thorough the object's properties:

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

Here is an example: http://jsfiddle.net/8Y9ru/

Upvotes: 3

Related Questions