Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create object array using attributes of list

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.

like image 870
user1937021 Avatar asked Jan 27 '26 04:01

user1937021


1 Answers

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);  
});
like image 104
ATOzTOA Avatar answered Jan 30 '26 07:01

ATOzTOA