0

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/

4
  • It's the for-in statement that's at issue. That's the part that doesn't guarantee the order. Instead use a for statement (or $.each) to iterate the dataObj.order Array, but instead of giving it indices, give it the name of each dataObj.sites property in the order you want to visit it. Commented Jun 14, 2014 at 20:52
  • ... order:["behance","cinqcentpx","dribbble"] and then you'll want to add the ID to each object too. Commented Jun 14, 2014 at 20:54
  • It's isn't clear at all for me. Because actually I already loop through dataObj.order with a each loop. Commented Jun 14, 2014 at 21:01
  • ...if the IDs are really meant to correspond to a zero-based index of each object, then an Array of Objects would seem to make much more sense. Commented Jun 14, 2014 at 21:03

2 Answers 2

1

I'm not positive what you are trying to do, but maybe this fiddle helps? http://jsfiddle.net/XhnyS/7/

Instead of using indexes and order list of stringed numbers, I simply changed the order list to the site keys themselves, then created one list item and added it to every .social-wrapper found.

dataObj = {
    "sites":{
        "dribbble":{
            "username":"JeremDsgn"
        },
        "behance":{
            "username":"JeremDsgn"
        },
        "cinqcentpx":{
            "username":"jeremdsgn"
        }
    },
    "order":["cinqcentpx", "behance", "dribbble"]
};

for (var i = 0; i < dataObj.order.length; i++) {
    var siteName = dataObj.order[i];
    var itemList = $('<li/>').attr('id', i).text(siteName + ': ' + dataObj.sites[siteName].username);
    $(itemList).appendTo('.list-social .social-wrapper');
}

Edited to actually answer the question :)

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

2 Comments

You're not using the dataObj.order list. The Object.keys() doesn't guarantee any particular order, just like for-in.
I should have only three li item, the other are duplicates and I didn't know how to don't repeat them. And I would rather keep number for order if possible.
0

Try this. It worked.

   $.each(dataObj.order, function(i, id) {
    console.log(i, id);
    var index = 0;  
    for (var site in dataObj.sites) {
       console.log(index);
        if(index==id){
          console.log("equal",id,index,site);
          var itemList = '<li id="' + index + '">' + site + '</li>';
          $(itemList).appendTo('.list-social .social-wrapper');
          break;
          }
    index++;
    }
});

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.