2

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!

Abbas
6,8944 gold badges38 silver badges49 bronze badges
asked Dec 27, 2011 at 8:56
2
  • You might want to look into something called "nested loops". It's been an invaluable tool during my career, and I hope it helps you too! Commented Dec 27, 2011 at 9:05
  • @user889349 - What is the criteria for the two separate arrays? You can loop the JSON string and use if/else to determine which array the data should go to then just push() to that array. Commented Dec 27, 2011 at 9:22

4 Answers 4

1

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);
 }
 }
}
answered Dec 27, 2011 at 9:24
Sign up to request clarification or add additional context in comments.

2 Comments

... chanels_u[j] Where is a j defined?
Oops. It should be in the second loop, I will update my response.
1

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"}]
}
answered Dec 27, 2011 at 9:39

Comments

0

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
}
answered Dec 27, 2011 at 9:04

1 Comment

Maybe. It works in my code. But it isn't right answer for my queston. Thanks :)
0

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*/
}
answered Dec 27, 2011 at 9:23

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.