0
objectx{propA: valueA,
        propB: valueB,
        propC: [{name: john,  id:123},
                {name: tom, id:456}]
       }
  1. 如何检查 in 的id: 789存在objectx.propC
  2. 如果 为空id: 789,如何检查是否存在?objectx.propCobjectx.propC

谢谢你的帮助!

4

1 回答 1

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
于 2013-10-29T01:50:34.877 回答