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.

share|improve this question
feedback

1 Answer

up vote 3 down vote accepted

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|improve this answer
A detailed explanation of what was wrong and what you've done to fix it would be fantastic. – Anthony Grist Jan 15 at 13:38
feedback

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.