gpt4 book ai didi

javascript - 关联数组/对象 - 使用点符号获取值

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:38:16 25 4
gpt4 key购买 nike

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

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

我还以为是相反的呢。

最佳答案

您需要使用 [] 符号以稍微不同的方式访问它,如下所示:

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

在 JavaScript 中,它们是等价的:

obj.Property
obj["Property"]

或者按照你的情况:

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

关于javascript - 关联数组/对象 - 使用点符号获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3400272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com