admin管理员组

文章数量:1387301

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.

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.

Share Improve this question edited Apr 18, 2014 at 18:08 Joshua Dwire 5,4515 gold badges31 silver badges51 bronze badges asked Aug 3, 2010 at 19:52 xgarbxgarb 331 silver badge3 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 7

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];

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.

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)");

本文标签: javascriptAssociative ArraysObjectgetting values with dot notationStack Overflow