0

I'd like if possible parse an angularJS model. Thats means, for each properties check one by one (in a loop) if the value of this property make an action if not make a different action.

I can do this in C# but don't know if possible in this language.

var customer = {
    FirstName: "",
    LastName: "MyLastName",
    Email: "",
}; 

Thanks,

Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
TheBoubou
  • 19,487
  • 54
  • 148
  • 236

2 Answers2

2

var customer = {
    FirstName: "",
    LastName: "MyLastName",
    Email: "",
}; 

for(var propertyName in customer) {
   var propertyValue = customer[propertyName];
   console.log('LIST PROPERTIES: ', propertyName, propertyValue);
   
   if(propertyName == "LastName" && propertyValue == "MyLastName") {
       // do something
       console.log('your ' + propertyName + ' is ' + propertyValue);
   }
}
Develoger
  • 3,950
  • 2
  • 24
  • 38
1

You can do this, using for(key in Obj) loop.

for(var propertyName in customer) {
   // you can get the value like this: $scope.customer[propertyName]
}
Krzysztof Safjanowski
  • 7,292
  • 3
  • 35
  • 47
Daria
  • 276
  • 1
  • 3
  • 13
  • If using the for() loop, it's recommended to verify that $scope.customer.hasOwnProperty(propertyName) returns true to avoid getting unwanted inheriting properties. – Elarcis Aug 14 '15 at 11:45