Jquery - Each with multiple arrays

Asked

Viewed 91 times

1

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

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

It would be something like:

$.each(data.id_site, data.name, function(i, item, item2) {
    $(".lado").append("<div id='site-" + item + "'>" + item2 + "</div>");
});
  • Can you explain the question better? Can you give an example of what the data and the result you hope to achieve?

  • I changed the question, I need a each with more than 1 array, that’s it!

  • So you want something like $(".lado").append("<div id='site-" + item + "'>" + name + "</div>"); that’s it?

  • Yes!!!!!!!!!!!!!

1 answer

2


Supposing that both data. and data.name have the same number of elements, you can iterate one of them as you are doing and use the 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 not to keep calling .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);
  • 1

    that’s right, vlw!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.