SoFunction
Updated on 2025-03-01

How to determine whether an object contains a certain attribute

1. Use the in keyword to return true/false and can be achieved by testing it personally!

//Create object data
var data = ;

// When determining that the update value is 'Yes', the data must have two fields 'userXM' and 'mobile', and neither is empty    if( == "yes"){
    if(!('userXM' in data)||!( 'mobile' in data)){
 //Execute code without fields   }else if(data["userXM"] == ""|| data["mobile"] == ""){

//The code executed by two values, or one of them is empty}else{
}
}
var obj = {name:'jack'};
alert('name' in obj); // --> true
alert('toString' in obj); // --> true

method

This method returns a Boolean value indicating whether the object's own properties have the specified attribute (that is, whether there is a specified key).

var obj = {name:'jack'};
('name'); // --> true
('toString'); // --> false

3. Use undefined to judge

var o={x:1};
!==undefined; //true
!==undefined; //false
!==undefined //true

The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.