Javascript jQuery 对象通过键获取值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11813800/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
jQuery object get value by key
提问by user1095118
How would you get the value of assocIMG
by key matching the key eg
您将如何assocIMG
通过与键匹配的键获得值,例如
if I have a var 11786
I want it to return media/catalog/product/8795139_633.jpg
如果我有一个 var11786
我希望它返回media/catalog/product/8795139_633.jpg
var spConfig = {
"attributes": {
"125": {
"id": "125",
"code": "pos_colours",
"label": "Colour",
"options": [{
"id": "236",
"label": "Dazzling Blue",
"price": "0",
"oldPrice": "0",
"products": ["11148"]
}, {
"id": "305",
"label": "Vintage Brown",
"price": "0",
"oldPrice": "0",
"products": ["11786", "11787", "11788", "11789", "11790", "11791", "11792", "11793"]
}]
}
}
};
var assocIMG = // Added - Removed { here, causes issues with other scripts when not working with a configurable product.
{
11786: 'media/catalog/product/8795139_633.jpg',
11787: 'media/catalog/product/8795139_633.jpg',
}
Above is the objects I am working with and below is my current jQuery. Help would be greatly appreciated.
上面是我正在使用的对象,下面是我当前的 jQuery。帮助将不胜感激。
$('#attribute125').change(function() {
var image = $(this).val();
$.each(spConfig.attributes, function() {
prods = $(this.options).filter( function() { return this.id == image; } )[0].products[0];
alert(prods);
});
});
回答by Bergi
You can use bracket notationto get object members by their keys. You have the variable prods
containing a string ("11786"
), and the object assocIMG
with various keys. Then just use
您可以使用括号表示法通过键获取对象成员。您有prods
包含字符串 ( "11786"
)的变量,以及assocIMG
具有各种键的对象。然后只需使用
assocIMG[prods]
to get the property value 'media/catalog/product/8795139_633.jpg'
which is associated with that key.
获取'media/catalog/product/8795139_633.jpg'
与该键关联的属性值。
Note that you should always use strings as keys in your object literal, IE does not support numbers there:
请注意,您应该始终使用字符串作为对象文字中的键,IE 不支持数字:
var assocIMG = {
"11786": 'media/catalog/product/8795139_633.jpg',
"11787": 'media/catalog/product/8795139_633.jpg'
};
Another improvement to your script would be not to loop through the spConfig.attributes
each time, and potentially execute your action multiple times if an image is contained in more than one attribute. Instead, build a hash object out of it, where you can just look up the respective product id.
对您的脚本的另一项改进是不循环遍历spConfig.attributes
每次,如果图像包含在多个属性中,则可能多次执行您的操作。相反,从中构建一个哈希对象,您可以在其中查找相应的产品 ID。
var productById = {};
$.each(spConfig.attributes, function() {
$.each(this.options, function() {
var id = this.id;
productsById[i] = this.products[0];
});
});
$('#attribute').change(function() {
var id = this.value;
var prod = productById[id];
var image = assocIMG[prod];
$("#product_img").attr("src", image);
});
回答by davidbuzatto
You should not use numbers as object keys (in their start). If you want to get the value associated with the 11786
integer key, you will need to use this syntax:
您不应该使用数字作为对象键(在它们的开头)。如果要获取与11786
整数键关联的值,则需要使用以下语法:
assocIMG["11786"] or assocIMG[11786]
Not
不是
assocIMG.11786
The first thing that you need to do is to create your keys as strings, since you would have:
您需要做的第一件事是将您的密钥创建为字符串,因为您将拥有:
var assocIMG = {
"11786": 'media/catalog/product/8795139_633.jpg',
"11787": 'media/catalog/product/8795139_633.jpg',
}
But even doing this, you won't be able to access the field using assocIMG.11786
and the first valid sintax that I presented will still work. The correct approach would be:
但即使这样做,您也无法使用该字段访问该字段,assocIMG.11786
并且我介绍的第一个有效语法仍然有效。正确的做法是:
var assocIMG = {
id11786: 'media/catalog/product/8795139_633.jpg',
id11787: 'media/catalog/product/8795139_633.jpg',
}
Or
或者
var assocIMG = {
"id11786": 'media/catalog/product/8795139_633.jpg',
"id11787": 'media/catalog/product/8795139_633.jpg',
}
Note that the keys are now starting with letters, not numbers. And now, you will can access the 11786
field as assocIMG.id11786
or assocIMG["id11786"]
, notassocIMG[id11786]
请注意,键现在以字母开头,而不是数字。现在,您可以使用or访问该11786
字段,而不是assocIMG.id11786
assocIMG["id11786"]
assocIMG[id11786]
回答by user1095118
To Get the Value from object by matching key I ended up with the following
要通过匹配键从对象中获取值,我最终得到以下结果
$.each(assocIMG, function(index, value) {
if(index == prods) {
var image_path = value;
$("#product_img").attr("src", image_path);
//alert(image_path);
}