简体   繁体   English

循环遍历嵌套的 js 对象并使用 Jquery 使用自定义 html 附加 div

[英]loop through nested js object and append div with custom html using Jquery

Im not even sure if I worded the title of what I need correctly, but in a nutshell, I'm trying to loop through a nested js object, extract specific values, combine them with an html template, then append a Div with the result (for an ecommerce site uing snipcart).我什至不确定我是否正确地说出了我需要的标题,但简而言之,我试图遍历嵌套的 js 对象,提取特定值,将它们与 html 模板组合,然后在结果中附加一个 Div (对于使用 snipcart 的电子商务网站)。

This is my code at the moment (which clearly doesnt work)这是我目前的代码(显然不起作用)

var comics = {
    modern: {
        1: {
            "data-item-id" : "pun-224",
            "data-item-price" : "3.49",
            "data-item-url" : "/",
            "data-item-description" : "The Punisher issue 224",
            "data-item-image" : "/img/modern.jpg",
            "data-item-name" : "The punisher 224"
        },
        2:{
            "data-item-id" : "pun-225",
            "data-item-price" : "3.49",
            "data-item-url" : "/",
            "data-item-description" : "The Punisher issue 224",
            "data-item-image" : "/img/silver.jpg",
            "data-item-name" : "The punisher 225"
        }
            

    }
}

$(document).ready(function(){
console.log("hello");
$.each(comics.modern, function() {
    
        var list = $("#comic__list");
        list.append(
            `<button 
            class="snipcart-add-item" 
            data-item-id="${this.data-item-id}" 
            data-item-price="${this.data-item-id}" 
            data-item-url="${this.data-item-id}" 
            data-item-description="${this.data-item-id}" 
            data-item-image="${this.data-item-id}" 
            data-item-name="${this.data-item-id}"> 
            Add to cart
            </button>`
        )    

    
});

}); });

Just use Object.keys to get the object keys as array and then you can loop through it:只需使用 Object.keys 将对象键作为数组获取,然后您就可以遍历它:

$(document).ready(function(){
$.each(Object.keys(comics.modern), function() {
    var currentObject = comics.modern[this];
    var list = $("#comic__list");
    list.append(
         `<button 
            class="snipcart-add-item" 
            data-item-id="${currentObject['data-item-id']}" 
            data-item-price="${currentObject['data-item-id']}" 
            data-item-url="${currentObject['data-item-id']}" 
            data-item-description="${currentObject['data-item-id']}" 
            data-item-image="${currentObject['data-item-id']}" 
            data-item-name="${currentObject['data-item-id']}"> 
            Add to cart
         </button>`
     )   
    
});
});

Note: you cannot use object.property-of-the-object when the property contains a dash, instead use object['property-of-the-object']注意:当属性包含破折号时,您不能使用 object.property-of-the-object,而是使用 object['property-of-the-object']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM