Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if an object has a property [duplicate]

How can I check if an object has a certain property in AngularJS?

like image 426
Daniel R Avatar asked Dec 09 '13 06:12

Daniel R


People also ask

How do you check if an array of objects has duplicate values in JavaScript?

There are multiple methods available to check if an array contains duplicate values in JavaScript. You can use the indexOf() method, the Set object, or iteration to identify repeated items in an array.

How do you check if an object contains a property?

We can check if a property exists in the object by checking if property !== undefined . In this example, it would return true because the name property does exist in the developer object.


1 Answers

You could use 'hasOwnProperty' to check if object have the specific property.

if($scope.test.hasOwnProperty('bye')){   // do this    }else{   // do this then } 

Here's a demo in jsFiddle.

Hope this helpful.

like image 174
Chickenrice Avatar answered Oct 23 '22 03:10

Chickenrice