objectx{propA: valueA,
propB: valueB,
propC: [{name: john, id:123},
{name: tom, id:456}]
}
id: 789
中是否存在objectx.propC
?id: 789
为空,如何检查objectx.propC
中是否存在objectx.propC
?感谢您的帮助!
答案 0 :(得分:0)
objectx = {propA: 'valueA',
propB: 'valueB',
propC: [{name: 'john', id:123},
{name: 'tom', id:456}]
}
function getIdInPropC(objectx,id) {
for (key in objectx.propC) {
var obj = objectx.propC[key];
if (!obj) {continue;}
if (obj.id && obj.id == id) {return obj;}
}
return false;
}
getIdInPropC(objectx, 456);//returns obj containing top and id 456
getIdInPropC(objectx, 777);//returns false