Find JSON Objects with Same Property and Separate Them

Introduction

This article explains how to find the objects with the same property from a JSON and separate them to be shown in the UI. I hope you will like it.

Please see this article in my blog: Find JSON Objects With Same Property and Separate Them.

Background


Yesterday I came across a situation where there was a need to find the objects with the same property from some JSON data and separate them. So I thought of sharing that with you all.

Using the code

First of all we will add a jQuery reference as in the following code snippet:
  1. <script src="jquery-2.1.4.min.js"></script>   
Now we need some data, right? Please have a look at the following JSON data.
  1. var data = '[{"name":2014,"data":[{"x":"1","y":222808746.81}]},{"name":2013,"data":[{"x":"2","y":289647045.18}]},{"name":2014,"data":[{"x":"2","y":285136890.07}]},{"name":2013,"data":[{"x":"3","y":370853178.74}]},{"name":2014,"data":[{"x":"3","y":403272964.28}]},{"name":2012,"data":[{"x":"4","y":217294031.36}]},{"name":2013,"data":[{"x":"4","y":224715039.94}]},{"name":2014,"data":[{"x":"4","y":249034460.23}]},{"name":2012,"data":[{"x":"5","y":215978054.15}]},{"name":2013,"data":[{"x":"5","y":241211810.92}]},{"name":2014,"data":[{"x":"5","y":245950715.8}]},{"name":2012,"data":[{"x":"6","y":262898180.85}]},{"name":2013,"data":[{"x":"6","y":280550623.51}]},{"name":2014,"data":[{"x":"6","y":295780079.03}]},{"name":2012,"data":[{"x":"7","y":217310275.85}]},{"name":2013,"data":[{"x":"7","y":230973675.47}]},{"name":2014,"data":[{"x":"7","y":238227382.03}]}]';  
Next we need some UI elements as in the following.
  1. <div id="initialData"></div>    
  2.    <div id="newData"></div>    
  3.    <button id="separateme">Make Me Separate</button>   
What next? We need to show this data to our UI, right? For that I will call a function in our document ready function.
  1. $(function () {    
  2.     $('#newData').hide();    
  3.     loadinitialData();   
  4. });  
The following is the function definition for the loadinitialData() function.
  1. function loadinitialData()
  2. {    
  3.      jsonObject = $.parseJSON(data);    
  4.      var html = '<table><th>Year</th><th>X Value</th><th>Y Value</th>';    
  5.      for (i = 0; i < jsonObject.length; i++) {    
  6.          html += '<tr><td>' + jsonObject[i].name + '</td><td>' + jsonObject[i].data[0].x + '</td><td>' + jsonObject[i].data[0].y + '</td></tr>';    
  7.      }    
  8.      html += '</table>';    
  9.      $('#initialData').append(html);    
  10.  }    
What we are doing here is parsing our data and appending the data to our UI element using a for loop.

Now after running it, you will get the following output.

get output
We have successfully formatted our data and shown it to our UI. Cool, right?

So shall we go and separate our data? I guess you said "Yes". Awesome. Now we will create a click event for our "Make me sort" button as in the following:
  1. $('#separateme').click(function () {                   
  2.      loadnewData();    
  3.      $('#separateme').hide();    
  4.      $('#newData').show();    
  5.  });  
Here is the definition of the loadnewData() function.
  1. function loadnewData()
  2. {               
  3.       for (i = 0; i < uniqueNames.length; i++) 
  4.       {    
  5.           var currentName = uniqueNames[i];    
  6.           var uniqueDataArray = $.grep(jsonObject, function (data) {    
  7.               return data.name == currentName;    
  8.           });    
  9.           var html = '<table><th>Year</th><th>X Value</th><th>Y Value</th>';    
  10.           for (j = 0; j < uniqueDataArray.length; j++) {    
  11.               html += '<tr><td>' + uniqueDataArray[j].name + '</td><td>' + uniqueDataArray[j].data[0].x + '</td><td>' + uniqueDataArray[j].data[0].y + '</td></tr>';    
  12.           }    
  13.           html += '</table>';    
  14.           $('#newData').append(html);    
  15.       }    
  16.          
  17.   }  
Now I guess you could determine the difference of both the loadinitialData() and loadnewData() functions. Yeah, you are right, we are using a function "grep" there.
  1. var uniqueDataArray = $.grep(jsonObject, function (data) {    
  2.     return data.name == currentName;    
  3. });   
What our "grep" function does is, it will take all objects at a time that has the property "name" as currentName.
  1. return data.name == currentName;   
Once that is done, we will create a separate HTML table for each property and bind it to our parent element. Shall we now look into the complete code? We have done everything.

Complete Code
  1. <!DOCTYPE html>    
  2. <html>    
  3. <head>    
  4.     <title>Find JSON Object by its property,separate and show demo - Sibeesh Passion</title>    
  5.     <script src="jquery-2.1.4.min.js"></script>    
  6.     <style>    
  7.         #initialData {    
  8.             border: 1px solid #999;    
  9.             width:220px;    
  10.             padding:10px;    
  11.             float:left;    
  12.         }    
  13.         #newData {    
  14.             border: 1px solid #999;    
  15.             width:220px;    
  16.             padding:10px;    
  17.             float:left;    
  18.         }    
  19.          td {    
  20.             border: 1px solid #ccc;    
  21.             padding: 5px;    
  22.             text-align: center;    
  23.         }    
  24.     
  25.     </style>    
  26.     <script>      
  27.         var data = '[{"name":2014,"data":[{"x":"1","y":222808746.81}]},{"name":2013,"data":[{"x":"2","y":289647045.18}]},{"name":2014,"data":[{"x":"2","y":285136890.07}]},{"name":2013,"data":[{"x":"3","y":370853178.74}]},{"name":2014,"data":[{"x":"3","y":403272964.28}]},{"name":2012,"data":[{"x":"4","y":217294031.36}]},{"name":2013,"data":[{"x":"4","y":224715039.94}]},{"name":2014,"data":[{"x":"4","y":249034460.23}]},{"name":2012,"data":[{"x":"5","y":215978054.15}]},{"name":2013,"data":[{"x":"5","y":241211810.92}]},{"name":2014,"data":[{"x":"5","y":245950715.8}]},{"name":2012,"data":[{"x":"6","y":262898180.85}]},{"name":2013,"data":[{"x":"6","y":280550623.51}]},{"name":2014,"data":[{"x":"6","y":295780079.03}]},{"name":2012,"data":[{"x":"7","y":217310275.85}]},{"name":2013,"data":[{"x":"7","y":230973675.47}]},{"name":2014,"data":[{"x":"7","y":238227382.03}]}]';    
  28.         var jsonObject;    
  29.         var uniqueNames = [2013, 2012, 2014];    
  30.         $(function () {    
  31.             $('#newData').hide();    
  32.             loadinitialData();    
  33.             $('#separateme').click(function () {                   
  34.                 loadnewData();    
  35.                 $('#separateme').hide();    
  36.                 $('#newData').show();    
  37.             });    
  38.         });    
  39.         function loadinitialData() {    
  40.             jsonObject = $.parseJSON(data);    
  41.             var html = '<table><th>Year</th><th>X Value</th><th>Y Value</th>';    
  42.             for (i = 0; i < jsonObject.length; i++) {    
  43.                 html += '<tr><td>' + jsonObject[i].name + '</td><td>' + jsonObject[i].data[0].x + '</td><td>' + jsonObject[i].data[0].y + '</td></tr>';    
  44.             }    
  45.             html += '</table>';    
  46.             $('#initialData').append(html);    
  47.         }    
  48.         function loadnewData() {               
  49.             for (i = 0; i < uniqueNames.length; i++) {    
  50.                 var currentName = uniqueNames[i];    
  51.                 var uniqueDataArray = $.grep(jsonObject, function (data) {    
  52.                     return data.name == currentName;    
  53.                 });    
  54.                 var html = '<table><th>Year</th><th>X Value</th><th>Y Value</th>';    
  55.                 for (j = 0; j < uniqueDataArray.length; j++) {    
  56.                     html += '<tr><td>' + uniqueDataArray[j].name + '</td><td>' + uniqueDataArray[j].data[0].x + '</td><td>' + uniqueDataArray[j].data[0].y + '</td></tr>';    
  57.                 }    
  58.                 html += '</table>';    
  59.                 $('#newData').append(html);    
  60.             }    
  61.                
  62.         }      
  63.     </script>    
  64. </head>    
  65. <body>    
  66.     <div id="initialData"></div>    
  67.     <div id="newData"></div>    
  68.     <button id="separateme">Make Me Separate</button>    
  69. </body>    
  70. </html>    
Now it is time to see our output as in the following.

output
You can find we have separated our object and shown it in a separate table. Cool. That is all for the day. I will return with another article soon.

Conclusion

I hope you liked this article. Please share with me your valuable thoughts and comments. Your feedback is always welcomed.
Happy coding!