2

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.

seadrag0n
  • 848
  • 1
  • 16
  • 40
  • possible duplicate of [How to display all methods in a JavaScript object?](http://stackoverflow.com/questions/2257993/how-to-display-all-methods-in-a-javascript-object) – Paddy Jun 17 '14 at 08:56
  • You have to work on the instance: `var obj = new AdvancedFilters(); for (var prop in obj) {}` – GôTô Jun 17 '14 at 08:56
  • is for loop working on function? have you really tried out this? – Akhlesh Jun 17 '14 at 09:00
  • @Akhlesh the loop is working but I am storing the JS object in a knockout observable array using the `new` keyword so I have to check if the loop works on an observable array. Right now busy with other things but I will surely check this and update here later.... – seadrag0n Jun 24 '14 at 12:09

1 Answers1

6

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);
}
GôTô
  • 7,974
  • 3
  • 32
  • 43
  • That's clearly what he meant. He surely doesn't want to iterate the attributes of a function (although this is technically possible: A function can have attributes, too). – rplantiko Jun 17 '14 at 09:02