我正在使用twitter annotations api的最新草稿。数据的例子有点貌似从JavaScript散列中获取密钥
status {
annotations : [
{myAnnotationType:{myKey:myValue}},
{someoneElsesAnnotationType:{theirKey:theirValue}},
]
}
现在我要检查状态,看它是否有与它myAnnotationType注解。如果注释是散列而不是数组,我只能写var ann = status.annotations.myAnnotationType。但它不是我写的这个:
function getKeys(obj){
var keys = [];
for (key in obj) {
if (obj.hasOwnProperty(key)) { keys[keys.length] = key; }
}
return keys;
}
function getAnnotation(status, type){
for(var i=0;i<status.annotations.length;i++){
var keys = getKeys(status.annotations[i]);
for(var j=0;j<keys.length;j++){
if(keys[j] == type){
return status.annotations[i];
}
}
}
}
var ann = getAnnotation(status, "myAnnotationType");
必须有更好的办法!在那儿?
PS我不能使用jquery或任何东西,因为这是JS在卡哈小部件使用和容器不支持外部库