关联数组/对象 - 使用点表示法获取值
我真的不明白为什么这不起作用:
thing = {
img78:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 450D", exifexposuretime:"1/125", exiffstop:"71/10", exifiso:"200"},
img79:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 550D", exifexposuretime:"1/125", exiffstop:"71/10", exifiso:"100"},
img80:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 550D", exifexposuretime:"1/30", exiffstop:"16/1", exifiso:"250"},
img81:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"},
img82:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/2500", exiffstop:"90/10", exifiso:"800"},
img83:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"},
img77:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 450D", exifexposuretime:"1/160", exiffstop:"8/1", exifiso:"100"},
img69:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"}
};
var imageid = 'img80';
console.log('myVar1: ', thing.img80.exifmodel);
console.log('myVar2: ', thing.imageid.exifmodel);
输出:
myVar1: Canon EOS 550D
thing.imageid is undefined
我本以为会是相反的情况。
I don't really get why this isn't working:
thing = {
img78:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 450D", exifexposuretime:"1/125", exiffstop:"71/10", exifiso:"200"},
img79:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 550D", exifexposuretime:"1/125", exiffstop:"71/10", exifiso:"100"},
img80:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 550D", exifexposuretime:"1/30", exiffstop:"16/1", exifiso:"250"},
img81:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"},
img82:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/2500", exiffstop:"90/10", exifiso:"800"},
img83:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"},
img77:{ exifmanufacturer:"Canon", exifmodel:"Canon EOS 450D", exifexposuretime:"1/160", exiffstop:"8/1", exifiso:"100"},
img69:{ exifmanufacturer:"NIKON CORPORATION", exifmodel:"NIKON D700", exifexposuretime:"10/600", exiffstop:"71/10", exifiso:"800"}
};
var imageid = 'img80';
console.log('myVar1: ', thing.img80.exifmodel);
console.log('myVar2: ', thing.imageid.exifmodel);
Outputs:
myVar1: Canon EOS 550D
thing.imageid is undefined
I would have thought it would be the other way round.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在第二个示例中,就像编写 thing.'img80'.exifmodel 一样,这是非法的。如果你想使用字符串来访问对象的字段,你必须执行 thing[imageid].exifmodel。
In the second example, it'd be like writing thing.'img80'.exifmodel which is illegal. If you want to use a string to access a field of an object you'd have to do thing[imageid].exifmodel.
当你在这样的字符串中有索引时,你必须使用括号表示法来访问值:
或者你总是可以采取 eval (或邪恶,取决于你认为这种做法有多糟糕)路线:
When you have the index in a string like that, you have to use the bracket notation to access the value:
Or you could always take the eval (or evil, depending on how bad you consider this practice) route:
您需要使用
[]
表示法稍微不同地访问它,如下所示:在 JavaScript 中,这些是等效的:
或者如您的情况:
You need to access it slightly differently using
[]
notation, like this:In JavaScript these are equivalent:
Or as in your case: