2

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.

1 Answer 1

11

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);  
});
Sign up to request clarification or add additional context in comments.

1 Comment

A detailed explanation of what was wrong and what you've done to fix it would be fantastic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.