Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery对象按键获取值_Javascript_Jquery - Fatal编程技术网

Javascript jQuery对象按键获取值

Javascript jQuery对象按键获取值,javascript,jquery,Javascript,Jquery,您如何通过与键匹配的键来获得assocIMG的值 如果我有一个var11786我希望它返回media/catalog/product/8795139_633.jpg var spConfig = { "attributes": { "125": { "id": "125", "code": "pos_colours", "label": "Colour", "options":

您如何通过与键匹配的键来获得
assocIMG
的值

如果我有一个var
11786
我希望它返回
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',
    } 
上面是我正在使用的对象,下面是我当前的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);

    });

});

不应将数字用作对象关键点(在其开始处)。如果要获取与
11786
integer键关联的值,需要使用以下语法:

assocIMG["11786"] or assocIMG[11786]
不是

assocIMG.11786
您需要做的第一件事是将键创建为字符串,因为您需要:

var assocIMG = {
    "11786": 'media/catalog/product/8795139_633.jpg',
    "11787": 'media/catalog/product/8795139_633.jpg',
} 
但即使这样做,您也无法使用
assocIMG.11786
访问该字段,并且我介绍的第一个有效的sintax仍然有效。正确的做法是:

var assocIMG = {
    id11786: 'media/catalog/product/8795139_633.jpg',
    id11787: 'media/catalog/product/8795139_633.jpg',
}


请注意,键现在以字母开始,而不是以数字开始。现在,您可以访问
11786
字段作为
assocIMG.id11786
assocIMG[“id11786”]
assocIMG[id11786]

,通过匹配键I从对象获取值,结果如下

$.each(assocIMG, function(index, value) { 
        if(index == prods) {
             var image_path = value;
             $("#product_img").attr("src", image_path);
             //alert(image_path); 
        }
可以使用通过对象成员的键获取对象成员。变量
prods
包含一个字符串(
“11786”
),对象
assocIMG
,带有各种键。那就用

assocIMG[prods]
获取与该键关联的属性值
'media/catalog/product/8795139_633.jpg'

请注意,您应该始终在对象文字中使用字符串作为键,即不支持数字:

var assocIMG = {
    "11786": 'media/catalog/product/8795139_633.jpg',
    "11787": 'media/catalog/product/8795139_633.jpg'
};

脚本的另一个改进是,不要每次循环通过
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);
});

你能解释得更清楚些吗?我很困惑:在产品数组中有很多产品ID,但只有2个图像?数字是有效的javascript对象文本键,只有IE不支持。但是,当写为字符串时,没有理由不使用它们。@Bergi您能解释一下为什么它是错误的吗?没有理由只为了访问一个特定的、已知的密钥而枚举对象。就像你不会为了访问数组的第i个索引而迭代数组一样。
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);
});