create object array using attributes of list

15,385

Object should be defined inside curly braces {}. Keys should be in quotes.

Working code:

$('ul.list').each(function() {
    var localproducts = [];
    $(this).find('li').each(function(){
            var $itm = $(this);
            localproducts.push({
                'dataid' : $itm.attr('data-id'), 
                'data-package' : $itm.attr('data-package'), 
                'package-id' : ($itm.children('.packageid').text())
            });
        });
    catalogue.push(localproducts);  
});
Share:
15,385
user1937021
Author by

user1937021

Updated on June 13, 2022

Comments

  • user1937021
    user1937021 almost 2 years

    I want to create an object array from values/attributes in a list, but the following doesn't work:

    $('ul.list').each(function() {
            var localproducts = [];
            $(this).find('li').each(function(){
                    var $itm = $(this);
                    localproducts.push( dataid : $itm.attr('data-id'), data-package: $itm.attr('data-package'), package-id: ($itm.children('.packageid').text()) );
                });
            catalogue.push(localproducts);  
    
            });
    

    Thanks for the help.

  • Anthony Grist
    Anthony Grist over 11 years
    A detailed explanation of what was wrong and what you've done to fix it would be fantastic.