I have an array ob objects like this[Object { cat="4", attr={"att1":"val1","att2":"val2","att3":"val3"}}, Object { cat="2", attr={"att1":"val1","att2":"val2","att3":"val3"}}, Object { cat="3", attr={"att1":"val1","att2":"val2","att3":"val3"}}, Object { cat="2",attr={"att1":"val1","att2":"val2","att3":"val3"}}, Object { cat="3", attr={"att1":"val1","att2":"val2","att3":"val3"}}]
and I have series of divs with child divs as follow
<div class="container" data-cat="1">
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
</div>
<div class="container" data-cat="2">
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
</div>
<div class="container" data-cat="3">
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
</div>
<div class="container" data-cat="4">
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
<div class="zone" data-product=""></div>
</div>
What I want to do is to loop through the objects array and through thr divs with class "container" and see if the object inside the array has the "cat" properity similar to the data-cat attribute of the container class div , then I go inside this specific div and
output some values to the child elements with class "zone" from the attr properity of the object like inserting data-product from a value inside the attr object which corresponds to the cat value equals to the data-cat of the parent
Note That I must output these values to number of divs with class zone equals to the number of the objects wich has cat property equals to its parent data-cat attribute
for example , if I have 2 object with cat 3 , then the container class will have 2 divs with class zone filled and 2 divs are empty and so on
here is what I did , but it did not work
// where ct is the array of objects
$(ct).each(function(i,item){
$(" div.container[data-cat='"+item.cat+"']").each(function(k,el){
if ($(el).attr("data-cat") == item.cat) {
$(el).find(".zone").eq(i).html("")
.attr("data-product",ct[i].attr.att1)
.append("<a class='rm_pd'>X</a>")
.append("<img src='"+ct[i].attr.att2+"' width='"+$("div.zone").width()+"' />")
.append("<h4>"+ct[i].attr.att3+"</h4>");
}
});
});
}