关联数组/对象 - 使用点表示法获取值

发布于 2024-09-12 12:29:35 字数 1310 浏览 3 评论 0原文

我真的不明白为什么这不起作用:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

遮了一弯 2024-09-19 12:29:40

在第二个示例中,就像编写 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.

信愁 2024-09-19 12:29:40

当你在这样的字符串中有索引时,你必须使用括号表示法来访问值:

var imageid = 'img80';
console.log('myVar2: ', thing[imageid].exifmodel);

或者你总是可以采取 eval (或邪恶,取决于你认为这种做法有多糟糕)路线:

eval("console.log('myVar2: '), thing." + imageid + ".exifmodel)");

When you have the index in a string like that, you have to use the bracket notation to access the value:

var imageid = 'img80';
console.log('myVar2: ', thing[imageid].exifmodel);

Or you could always take the eval (or evil, depending on how bad you consider this practice) route:

eval("console.log('myVar2: '), thing." + imageid + ".exifmodel)");
浅浅 2024-09-19 12:29:39

您需要使用 [] 表示法稍微不同地访问它,如下所示:

console.log('myVar2: ', thing[imageid].exifmodel);

在 JavaScript 中,这些是等效的:

obj.Property
obj["Property"]

或者如您的情况:

var prop = "Property";
obj[prop];

You need to access it slightly differently using [] notation, like this:

console.log('myVar2: ', thing[imageid].exifmodel);

In JavaScript these are equivalent:

obj.Property
obj["Property"]

Or as in your case:

var prop = "Property";
obj[prop];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文