0

I have the following JSON Object being loaded into my application and stored into a var called obj:

{
    "items" : [
    {
        "name" : "item-1",
        "group" : [
        {
            "groupName" : "name-1",
            "groupPosition" : 2
        },
        {
            "groupName" : "name-2",
            "groupPosition" : 1
        }]
    },
    {
        "name" : "item-2",
        "group" : [
        {
            "groupName" : "name-1",
            "groupPosition" : 1
        },
        {
            "groupName" : "name-2",
            "groupPosition" : 2
        }]
    }]
}

I then do the following to go through it:

var groups = new Array();
var items = new Array();
$.each(obj.items, function(i,r){
    var itemName = r.name;
    $.each(r.group, function(index, record){
        if ($.inArray(record.groupName) == -1) {
            groups.push(record.groupName);
            $('body').append('<div id="record.groupName"></div>');
        }
        $('#'+record.groupName).append('<div id="itemName">itemName</div>');
        // At this point I'm stuck as the items get added in order of iteration,
        // not according to their record.groupPosition value.
    });
});

There will eventually be several hundred "items" each contained within an unset number of "groups".

The trouble I'm having is how to iterate through the JSON object using jQuery or good ol'JavaScript and display the items in the correct position within each group as the items and groups won't be listed inside the JSON object in sequential order.

Any help would be greatly appreciated!

Thank you.

5
  • The ".sort()" function for arrays can be passed a function to be used as a comparator. Commented Oct 16, 2011 at 16:29
  • 3
    at what point you got stuck? reading the object? creating a new array? sorting it? this is a classic "do it for me" question. Commented Oct 16, 2011 at 16:29
  • Assuming this JSON originates at a server of your own control, have you considered doing the ordering there? Also, if you can, send an ARRAY of items rather than a OBJECT containing items. That way, the items can at least be sorted using normal sort routines on the front-end. Commented Oct 16, 2011 at 16:31
  • @mkoistinen Unfortunately I'm stuck with the object as it is used in other functions in the application. Sort routines would have been great. Commented Oct 16, 2011 at 16:36
  • @DvirAzulay Good point; I should have been clearer and provided more information. I've updated the OP. Commented Oct 16, 2011 at 17:08

3 Answers 3

2

Why not just give the group items the position index like this:

{
"items" : [
{
    "name" : "item-1",
    "group" : {
    2:{
        "groupName" : "name-1",
        "groupPosition" : 2
    },
    1:{
        "groupName" : "name-2",
        "groupPosition" : 1
    }}

},
{
    "name" : "item-2",
    "group" : {
    1:{
        "groupName" : "name-1",
        "groupPosition" : 1
    },
    2:{
        "groupName" : "name-2",
        "groupPosition" : 2
    }}
}]

}

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

2 Comments

Interesting... how would I then $.append() them to the Group div according to those positions? Wouldn't the $.each() function still go through them according to their order in the JSOn object?
No it will loop through them according the key value. give it a try
0

Assuming you have a variable which is assigned to this:

var data = ...

you could use the $.each() method:

$.each(data.items, function(index, item) {
    // here item.name will contain the name
    // and if you wanted to fetch the groups you could loop through them as well:
    $.each(item.group, function(i, group) {
        // here you can use group.groupName and group.groupPosition
    });
});

Arrays ([]) in javascript are 0 index based and preserve their order when you are iterating over them.

1 Comment

Sorry for not being clearer; I've updated the OP if you have any insight. Thank you!
0

If I understood correctly your problem it is not about the sorting it self but how to link them to your dom nodes, solution: use classes with numbers.

For example:

$(".group"+items[1].group[0].grouposition").append(items[1].group[0].name);
// this will give append to the element with class="group1"

If you join this with having the html structure that is being generated to match the same names, then it won't be a problem and you don't have to sort them

1 Comment

Sorry for not being clearer; I've updated the OP if you have any insight. Thank you!

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.