I tried to figure out how I can order items, inside a for..in loop, inside another each loop, looping through my order's array to respect.
I have an object with my data :
dataObj = {
"sites":{
"dribbble":{
// ID = 0
"username":"JeremDsgn"
},
"behance":{
// ID = 1
"username":"JeremDsgn"
},
"cinqcentpx":{
// ID = 2
"username":"jeremdsgn"
}
},
"order":["1","2","0"]
};
Actually I add the id for each items in the display loop, but can it be better to add the id in the object ?
So after, I tried to display my items with the order's array to respect.
var index = 0;
for (var site in dataObj.sites) {
var itemList = '<li id="' + index + '">' + site + '</li>';
$.each(dataObj.order, function(i, id) {
console.log(i, id);
$(itemList).appendTo('.list-social .social-wrapper');
});
index++;
}
But I duplicate my site 3 times, because, at this time I have 3 elements on my dataObj.sites object.
So how can I do to display my itemList with the good order? The final result should be :
<div class="list-social">
<ul class="social-wrapper">
<li id="1">behance</li>
<li id="2">cinqcentpx</li>
<li id="0">dribbble</li>
</ul>
</div>
FIDDLE : http://jsfiddle.net/JeremDsgn/XhnyS/
for-instatement that's at issue. That's the part that doesn't guarantee the order. Instead use aforstatement (or$.each) to iterate thedataObj.orderArray, but instead of giving it indices, give it the name of eachdataObj.sitesproperty in the order you want to visit it.order:["behance","cinqcentpx","dribbble"]and then you'll want to add the ID to each object too.