1

I'm looking at an ajax response and it contains a large json object, which contains an array, which contains json data like this:

"chat": {
    "chat": [
         {
        "chat_type": 0,
        "chat_id": 445473683,
        "chat_name": "system",
        etc,
        etc,
        "chat_talk": "Chat+messages+here"
        },
        {
        //Another chat message
        }

    ]
}

There are dozens of json entries inside the chat array, which only seems to have the 1 element. There are a few "chat_types" 0, 1, and 2. Now, what I want to do is select only chat entries that are of type 2 and do NOT have system as the "chat_name".

But the whole object in an array in an object confuses the heck out of me. How would I accomplish this?

EDIT: Code so far -

function modChat (clickEvent) {
var chatHistory = [];
var extraData = '{"planet_id":"6_300_6","item_config_version":"' + getChatTimeStamp() + '","count":20,"tick":"-1","language":"en"}';

//$("#chatBody").html(extraData);

runRequest(extraData, gameDataURL, function(response) {

    var results = [];
    var chats = JSON.stringify(response.chat.chat);
    for (var i = 0; i < chats.length; i++) {
        if (chats[i].chat_type == 2 && chats[i].chat_name != "system") {
            results.push(chats[i]);
        }
    }

    $("#chatBody").val(chats);

});//End postRequest

}
  • There is no such thing as a "json object in an array in a json object". All of this is _one_ json object (with a nested arrays and objects structure). – Jeremy Thille May 01 '15 at 05:49

2 Answers2

3

Nested objects and arrays shouldn't be confusing. Just do it one level at a time. To access a property, use .propname; to access an array element, use [index]. When they're nested, you just append them, so you can do json_obj.chat.chat[index] to get an element of the doubly-nested array.

And if you're going access a nested object repeatedly, you can use a variable to simplify it.

The rest is just a simple for loop.

var results = [];
var chats = json_obj.chat.chat;
for (var i = 0; i < chats.length; i++) {
    if (chats[i].chat_type == 0 && chats[i].chat_name != "system") {
        results.push(chats[i]);
    }
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • I appreciate the step by step, it helps. I setup this loop in my code and I'm getting the chat data, but it's givnig me chat_type 0s. I changed the loop condision to chats[i].chat_type == 2 and it still gives me 0 and it perplexs me. I should specify its giving me both type 0 and 2. And system lol –  May 01 '15 at 05:42
3

Use Array.prototype.filter:

var obj = {
    "chat": {
        "chat": [{
            "chat_type": 0,
                "chat_id": 445473683,
                "chat_name": "system",
                "chat_talk": "Chat+messages+here"
        }, {
            "chat_type": 2,
                "chat_id": 445473683,
                "chat_name": "system",
                "chat_talk": "Chat+messages+here"
        }, {
            "chat_type": 2,
                "chat_id": 445473683,
                "chat_name": "system1",
                "chat_talk": "Chat+messages+here"
        }]
    }
};

var res = obj.chat.chat.filter(function (item) {
    return item.chat_type == 2 && item.chat_name != 'system'
});

console.log(res);

JSFIDDLE.

Amir Popovich
  • 29,350
  • 9
  • 53
  • 99
  • 1
    I like this approach, filters are a forgotten about tool – ABrowne May 01 '15 at 05:26
  • 1
    Both answers were very informative! I appreciate both of your help. But this answer is a bit simpler than a loop and worked first try, Thank you guys! –  May 01 '15 at 05:58