seadrag0n
seadrag0n

Reputation: 848

Loop through JS object which has knockout observable properties

I have the following JS object:

function AdvancedFilters() {
    var self = this;
    self.AdvancedColId = ko.observable();
    self.AdvancedComapanyName = ko.observable();
    self.AdvancedClientCountry = ko.observable();
    self.AdvancedClientCity = ko.observable();
    self.AdvancedDatabaseLocation = ko.observable();
    self.AdvancedUserName = ko.observable();
    self.AdvancedEmail = ko.observable();
    self.AdvancedPhoneNo = ko.observable();
    self.AdvancedAccessFrom = ko.observable();
    self.AdvancedAccessTo = ko.observable();
    self.AdvancedCreatedOn = ko.observable();
    self.AdvancedCandidates = ko.observable();
    self.AdvancedErrorsReported = ko.observable();
    self.AdvancedActive = ko.observable();
    self.AdvancedRequestes = ko.observable();
}

I have to loop through all the properties present in the AdvancedFilters object and do something which applies to all the properties instead of accessing each property separately. I have to something like this:

for (var property in AdvancedFilters) {         
         // do something with property     
}

I tried the above syntax but it is not working.

Upvotes: 2

Views: 1908

Answers (1)

GôTô
GôTô

Reputation: 8053

Try this

function AdvancedFilters() {
    var self = this;
    self.AdvancedColId = ko.observable();
    //...
}

var obj = new AdvancedFilters();
for (var property in obj) {         
    alert(ko.isObservable(obj[property]) ? obj[property]() : property);
}

Upvotes: 6

Related Questions