JQuery - Each with multiple arrays

1

I need to make a each with several items, for example:

How can I do this? The "item" already generates the result of the array "data.id_site" but I need a new array together, "item2" which generates the result of the array "data.name".

It would be something like:

$.each(data.id_site, data.name, function(i, item, item2) {
    $(".lado").append("<div id='site-" + item + "'>" + item2 + "</div>");
});
    
asked by anonymous 27.07.2016 / 17:50

1 answer

2

Assuming that both data. and data.name have the same number of elements, you can iterate one of them as you are doing and use indice to get what you want from the other array. So:

$.each(data.id_site, function(i, item) {
    $(".lado").append("<div id='site-" + item + "'>" + data.name[i] + "</div>");
});

But to not always call .append() it is better to do so:

var divs = data.id_site.map(function(id, i){
    return "<div id='site-" + id+ "'>" + data.name[i] + "</div>"
}).join('');
$(".lado").append(divs);
    
27.07.2016 / 17:57