looping through JSON array in a jQuery list

前端 未结 4 1335
长情又很酷
长情又很酷 2021-01-12 19:10

I have this JSON array

// Json array
var productList = {\"products\": [
    {\"description\": \"product 1\", \"price\": \"9.99\"},
    {\"description\": \"pr         


        
4条回答
  •  广开言路
    2021-01-12 19:39

    Since you're already using jQuery, why don't you use the $.each() function?

    Instead of this code:

    var listItem = document.createElement('li');
    listItem.setAttribute('id', 'listitem');
    
    listItem.innerHTML = productList.products[0].description;
    
    $(list).append(listItem);
    

    Use this:

    $(productList.products).each(function(index){
        $(list).append('
  • ' + this.description + " = " + this.price + '
  • '); });

提交回复
热议问题