XTRUST.ORG
XTRUST.ORG

Reputation: 3402

Javascript | JSON and ARRAY

May be you can help me with my task. I have a JSON string:

{
    "chats":[
        {"id":"1","time":"13:02", "from_id":"2692","text":"1","to_id":"62"},
        {"id":"2","time":"13:48", "from_id":"62","text":"Hello!","to_id":"2692"},
        {"id":"12","time":"15:47", "from_id":"2692","text":"3","to_id":"62"},
        {"id":"13","time":"15:48", "from_id":"62","text":"4","to_id":"2692"},
        {"id":"30","time":"08:57", "from_id":"2692","text":"To me","to_id":"62"},
        {"id":"31","time":"09:28", "from_id":"66","text":"From user1","to_id":"2692"},
        {"id":"32","time":"09:29", "from_id":"66","text":"From user1","to_id":"2692"},
        {"id":"32","time":"09:29", "from_id":"2692","text":"From user1","to_id":"66"}
    ],
    "lastid": "32"
}

var my_id = 2692 /*My chats*/

/*Success JSON request below*/

onSuccess: function(m){
    var messages = [];
    var chanels = [];
    var chanels_u = [];
    for(var i=0; i<m.chats.length;i++){                     
        if (my_id != '' && m.chats[i].from_id != parseInt(my_id)){
            chanels.push(m.chats[i].from_id);
        }
    }
    chanels_u = chanels.unique(); /*We get id's: 62,66*/
}

My Question:

How can I create a new arrays dynamically (2 for this example for: 62 and 66) and push messages?

To 1 (62):

{"id":"1","time":"13:02", "from_id":"2692","text":"1","to_id":"62"},
{"id":"2","time":"13:48", "from_id":"62","text":"Hello!","to_id":"2692"},
{"id":"12","time":"15:47", "from_id":"2692","text":"3","to_id":"62"},
{"id":"13","time":"15:48", "from_id":"62","text":"4","to_id":"2692"},
{"id":"30","time":"08:57", "from_id":"2692","text":"To me","to_id":"62"}

To 2 (66):

{"id":"31","time":"09:28", "from_id":"66","text":"From user1","to_id":"2692"},
{"id":"32","time":"09:29", "from_id":"66","text":"From user1","to_id":"2692"},
{"id":"32","time":"09:29", "from_id":"2692","text":"From user1","to_id":"66"}

Thanks!

Upvotes: 2

Views: 215

Answers (4)

Abbas
Abbas

Reputation: 6886

You are already identifying your chat ID and pushing it into the chanels array in your code:

if (my_id != '' && m.chats[i].from_id != parseInt(my_id)){
            chanels.push(m.chats[i].from_id);
}

You are also identifying unique IDs in your code:

chanels_u = chanels.unique();

All you need to do is locate your chats from the JSON object by comparing them to the IDs in chanels_u and if there's a match, push the text field to the messages array. This is should be close to what you require:

for(var i=0; i<m.chats.length;i++){                     
    for(var j=0; j<chanels_u.length;j++){
        if (my_id !== '' && m.chats[i].from_id === chanels_u[j]){
            messages.push(m.chats[i].text);
        }
    }
}

Upvotes: 1

Dimitar Christoff
Dimitar Christoff

Reputation: 26165

this will do what you want, i believe - create a new object with keys of the to_id and from_id in a way that allows you to loop them whichever way you want. it would probably make more sense to have a toUser and fromUser objects that you can loop independently and cross reference as you build the replies but it's trivial to do so.

var chats = {
    "chats":[
        {"id":"1","time":"13:02", "from_id":"2692","text":"1","to_id":"62"},
        {"id":"2","time":"13:48", "from_id":"62","text":"Hello!","to_id":"2692"},
        {"id":"12","time":"15:47", "from_id":"2692","text":"3","to_id":"62"},
        {"id":"13","time":"15:48", "from_id":"62","text":"4","to_id":"2692"},
        {"id":"30","time":"08:57", "from_id":"2692","text":"To me","to_id":"62"},
        {"id":"31","time":"09:28", "from_id":"66","text":"From user1","to_id":"2692"},
        {"id":"32","time":"09:29", "from_id":"66","text":"From user1","to_id":"2692"},
        {"id":"32","time":"09:29", "from_id":"2692","text":"From user1","to_id":"66"}
    ],
    "lastid": "32"
};

var newchats = {}, my_id = 2692;

chats.chats.each(function(chat) {
    if (!chat.id || !chat.id.length || chat.from_id == my_id)
        return;

    newchats["from" + chat.from_id] = newchats["from" + chat.from_id] || [];
    newchats["from" + chat.from_id].push(chat);

    newchats["to" + chat.to_id] = newchats["to" + chat.to_id] || [];
    newchats["to" + chat.to_id].push(chat);

});

console.log(newchats, JSON.encode(newchats));

see on jsfiddle http://jsfiddle.net/dimitar/7j2SN/, outputs:

{
    "from62": [{
        "id": "2",
        "time": "13:48",
        "from_id": "62",
        "text": "Hello!",
        "to_id": "2692"},
    {
        "id": "13",
        "time": "15:48",
        "from_id": "62",
        "text": "4",
        "to_id": "2692"}],
    "to2692": [{
        "id": "2",
        "time": "13:48",
        "from_id": "62",
        "text": "Hello!",
        "to_id": "2692"},
    {
        "id": "13",
        "time": "15:48",
        "from_id": "62",
        "text": "4",
        "to_id": "2692"},
    {
        "id": "31",
        "time": "09:28",
        "from_id": "66",
        "text": "From user1",
        "to_id": "2692"},
    {
        "id": "32",
        "time": "09:29",
        "from_id": "66",
        "text": "From user1",
        "to_id": "2692"}],
    "from66": [{
        "id": "31",
        "time": "09:28",
        "from_id": "66",
        "text": "From user1",
        "to_id": "2692"},
    {
        "id": "32",
        "time": "09:29",
        "from_id": "66",
        "text": "From user1",
        "to_id": "2692"}]
}

Upvotes: 1

Diode
Diode

Reputation: 25145

You can make chanels and object instead of array.

onSuccess: function(m){
    var messages = [];
    var chanels = {};
    var chanels_u = [];
    for(var i=0; i<m.chats.length;i++){                     
        if (my_id != '' && m.chats[i].from_id != parseInt(my_id)){
            var fromID = m.chats[i].from_id;
            if(!chanels[fromID]){
                chanels[fromID] = [];
            }
            chanels[fromID].push(m.chats[i]);
        }
    }
    //chanels_u = chanels.unique(); /*We get id's: 62,66*/
}

Upvotes: 0

Umesh Patil
Umesh Patil

Reputation: 10685

I guess, JSON is invalid. You missed to close the JSON properly.

{
    "chats": [
        {
            "id": "1",
            "time": "13:02",
            "from_id": "2692",
            "text": "1",
            "to_id": "62"
        },
        {
            "id": "2",
            "time": "13:48",
            "from_id": "62",
            "text": "Hello!",
            "to_id": "2692"
        },
        {
            "id": "12",
            "time": "15:47",
            "from_id": "2692",
            "text": "3",
            "to_id": "62"
        },
        {
            "id": "13",
            "time": "15:48",
            "from_id": "62",
            "text": "4",
            "to_id": "2692"
        },
        {
            "id": "30",
            "time": "08:57",
            "from_id": "2692",
            "text": "To me",
            "to_id": "62"
        },
        {
            "id": "31",
            "time": "09:28",
            "from_id": "66",
            "text": "From user1",
            "to_id": "2692"
        },
        {
            "id": "32",
            "time": "09:29",
            "from_id": "66",
            "text": "From user1",
            "to_id": "2692"
        }
    ]
}

Use JSONLINT to validate your data. You can update(get/set) this array dynamically as you wish.

var msgBox={"chats":[]};
var msg={"id":1,"time":(new Date()).getTime(),"from_id":"","text":"","to_id":""};

var info={ // whole JSON data }
var chats=info['chats'];

for(m in chats){
  console.log(chats[m].text) // will display the text
  console.log(chats[m].time) // will display the time of chat  
  chat[m].to_id=32424;   // you can modify the to_id value
}

Upvotes: 0

Related Questions