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,

2

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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]
}

1 Comment

If using the for() loop, it's recommended to verify that $scope.customer.hasOwnProperty(propertyName) returns true to avoid getting unwanted inheriting properties.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.