0

I got a pretty simple code, but I bet it's really bad written. I tried to create some objects inside an array, and then build a list using jQuery .each() method.

I think this is stupidly easy, but I'm just learning...

This is the jQuery code I've written:

$(document).ready(function () {
    //items
    var armas = {
        BK: {
            mano_izq: [{
                type: 2,
                index: 5,
                name: "juan"
            }, {
                type: 2,
                index: 5,
                name: "juancho"
            }]
        },
        MG: {
            mano_der: [{
                type: 2,
                index: 5,
                name: "juan2"
            }, {
                type: 2,
                index: 5,
                name: "juan3"
            }]
        }
    };

    //controles
    $("#selec").on("click", function (e) {
        e.preventDefault();
        $(".itemlist").each(armas.BK.mano_izq, function (i, value) {
            $(this).append('<li>'+ value.name +'</li>');
        });
    });
});

As you can see, in that example I'm trying to build this list (without success, sadly)

<ul class="itemlist">
    <li>juan</li>
    <li>juancho</li>
    <li>juan2</li>
    <li>juan3</li>
</ul>

You can check my fiddle here: http://jsfiddle.net/Frondor/xbcxy8ur/

4 Answers 4

1

You should iterate through your objects, not through the list.

$("#selec").on("click", function (e) {
    e.preventDefault();
    var $itemlist = $(".itemlist");
    $.each(armas.BK.mano_izq, addLi);
    $.each(armas.MG.mano_der, addLi);
    function addLi(i, elem){
        $itemlist.append('<li>'+ elem.name +'</li>');
    }
});

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

I preffer to use the $.map() method over this, but +1 for the caching the addLi function
Well, actually $.map() has another intention. This method designed to translate one array to another one, not just to iterate through it even though translation include iteration. But if you wanna write more obvious (== better maintainable) code use API methods for its intended purpose. Unfortunately with jQuery $.map() is really more handy then $.each() due to flaw in $.each() design (iterator as first argument) but in other "swiss-knife" libraries (such as underscore) each definitely better.
1

jQuery's .each() iterates over jQuery-objects. For regular objects, you'll need to use $.each(), which can iterate over any collection.

$("#selec").on("click", function (e) {
    e.preventDefault();
    $.each(armas.BK.mano_izq, function (i, value) {
        $('.itemlist').append('<li>'+ value.name +'</li>');
    });
    $.each(armas.MG.mano_der, function (i, value) {
        $('.itemlist').append('<li>'+ value.name +'</li>');
    });
});

2 Comments

Can you tell me what's the difference between jQuery-objects and regular ones? Is there any difference on syntax?
jQuery-objects are DOM-elements (such as divs, paragraphs, headers etc.) wrapped in a jQuery container, which includes all the jQuery-methods (such as .each(), .html(), .append()...). You can read more about them here: learn.jquery.com/using-jquery-core/jquery-object.
1

You can use $.map to normalize your data, like this

var data = $.map(Object.keys(armas), function (el) {
   return $.map(Object.keys(armas[el]), function (key) {
       return armas[el][key];  
   });
});

$("#selec").on("click", function (e) {
    e.preventDefault();

    var list = $.map(data, function (el) {
        return '<li>' + el.name + '</li>';
    });

    // only once dom operation 
    $('.itemlist').html(list);
});

Example

Comments

0
$.each(armas.BK.mano_izq, function (i, value) {
   $(".itemlist").append('<li>'+ value.name +'</li>');
});

$.each(armas.MG.mano_der, function (i, value) {
        $(".itemlist").append('<li>'+ value.name +'</li>');
});

Demo

Comments

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.