Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Searched and tried with no luck, I guess it's time to ask ;-)

A JSON array is sent with Objects as items, inside each object, there are values associated with keys, i.e.

[{"name":"Jack", "message":"Hello!"},
 {"name":"John", "message":"Hi!"},
 {"name":"Jack", "message":"Hello Again!"}] 

I am using

$.each(json, function(){//--Do a Listing--//});

which renders a list like:

<ul>
    <li><span>Jack:</span>Hello!</li>
    <li><span>John:</span>Hi!</li>
    <li><span>Jack:</span>Hello Again!</li>
</ul> 

as looping all items out. However, if you have noticed that inside the JSON package, there will be duplicated keys - 'name' which I would like, instead of making a separated listing item, to make them stack together, e.g. to display as 'Jack +1' which is something like:

<ul>
    <li><span>Jack: (+1)</span>Hello!</li>
    <li><span>John:</span>Hi!</li>
</ul> 

So far, the looping is not a problem. The problem is how to compare the value with the same key while looping them out and then do something with them.

Thanks in advance!

share|improve this question
 
And what will happen with the second message from Jack? –  daver Jul 5 at 14:40
 
the <li> actually comes with a onClick() which provides a detailed page for that conversation. –  user2553986 Jul 5 at 15:34
 
Ok, so you actually dont need all the messages at the same time, so maybe you data structure is not right, like @Vladimir says in his answer –  daver Jul 5 at 15:38
 
the original JSON pack structure is fixed on the server-side, generated by PHP and it is out of my hands, unfortunately. –  user2553986 Jul 5 at 17:54

2 Answers

I would recommend you to build another one object according to the one you have, but in more convinient form. Like this one:

{
   "Jack": {
      "message": "Hello!",
      "times": 2
   },
   "John": {
      "message": "Hey",
      "times": 1
   }
}

So your programm will have two loops. First one which simplifies initial array, and second one loop renders html.

share|improve this answer
 
Thanks for your reply. If I am correct, what you have meant is for me to change the structure of the JSON package. That is something I could not. Otherwise, if I follow you correctly, I need to loop out the original JSON package, then restructure it into what you have suggested, then use 2 loops to do the final render. Am I right? –  user2553986 Jul 5 at 17:52
 
One loop to restructure and another one to render. Take a look at @daver answer, it will give you basic idea. –  Vladimir Kurijov Jul 8 at 11:52

You should do something like this:

var data = [{"name":"Jack", "message":"Hello!"},
   {"name":"John", "message":"Hi!"},
   {"name":"Jack", "message":"Hello Again!"}];
var messages = [];

for(var i=0; i < data.length; i++){
   if(!messages[data[i].name]){
      messages[data[i].name] = 0;
   }
   messages[data[i].name] += 1;
}

console.log(messages);

/*Output
   [Jack: 2, John: 1]
*/

In the case that you dont need the messages at the same time

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.