var obj = {a:"property 1",b:"property 2"} // Or whatever object you want to check.
if(Object.keys(obj).length == 2 // If the object only has 2 keys,
&& obj["FruitID"] // And FruitID exists as property of the object,
&& obj["PeachID"]){ // And PeachID exists as property of the object,
// The object only contains FruitID & PeachID;
}
または、関数でラップします。
function isBaseObject(obj){
return !!(Object.keys(obj).length == 2 && obj["FruitID"] && obj["PeachID"]); // !! to cast the output to a boolean
}
isBaseObject({FruitID:"property 1",PeachID:"property 2"})
//true
isBaseObject({FruitID:"property 1",PeachID:"property 2", a:1})
//false
isBaseObject({a:1})
//false