<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>JSON Object &#8211; Sibeesh Passion</title>
	<atom:link href="https://www.sibeeshpassion.com/tag/json-object/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.sibeeshpassion.com</link>
	<description>My passion towards life</description>
	<lastBuildDate>Fri, 17 Jul 2015 12:31:04 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>/wp-content/uploads/2017/04/Sibeesh_Passion_Logo_Small.png</url>
	<title>JSON Object &#8211; Sibeesh Passion</title>
	<link>https://www.sibeeshpassion.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>Find JSON Objects With Same Property and Separate Them</title>
		<link>https://www.sibeeshpassion.com/find-json-objects-with-same-property-and-separate-them/</link>
					<comments>https://www.sibeeshpassion.com/find-json-objects-with-same-property-and-separate-them/#disqus_thread</comments>
		
		<dc:creator><![CDATA[SibeeshVenu]]></dc:creator>
		<pubDate>Fri, 29 May 2015 20:53:03 +0000</pubDate>
				<category><![CDATA[JQuery]]></category>
		<category><![CDATA[Json]]></category>
		<category><![CDATA[JSON Object]]></category>
		<category><![CDATA[Object By Property]]></category>
		<guid isPermaLink="false">https://sibeeshpassion.com/?p=2171</guid>

					<description><![CDATA[Introduction Today, 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. 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: [js] &#60;script src=“jquery-2.1.4.min.js”&#62;&#60;/script&#62; [/js] Now we need some data, right? Please have a look [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><strong>Introduction</strong></p>
<p>Today, 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.</p>
<p><strong>Background</strong></p>
<p>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.</p>
<p><strong>Using the code</strong></p>
<p>First of all we will add a jQuery reference as in the following code snippet:</p>
<p>[js]<br />
&lt;script src=“jquery-2.1.4.min.js”&gt;&lt;/script&gt;<br />
[/js]</p>
<p>Now we need some data, right? Please have a look at the following JSON data.</p>
<p>[js]<br />
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}]}]’;<br />
[/js]</p>
<p>Next we need some UI elements as in the following:</p>
<p>[html]<br />
&lt;div id=“initialData”&gt;&lt;/div&gt;<br />
   &lt;div id=“newData”&gt;&lt;/div&gt;<br />
   &lt;button id=“separateme”&gt;Make Me Separate&lt;/button&gt;<br />
[/html]</p>
<p>What next? We need to show this data to our UI, right? For that I am calling a function in our document ready function.</p>
<p>[js]<br />
$(function () {<br />
        $(‘#newData’).hide();<br />
        loadinitialData();<br />
    });<br />
[/js]</p>
<p>The following is the function definition for the loadinitialData() function.</p>
<p>[js]<br />
function loadinitialData() {<br />
     jsonObject = $.parseJSON(data);<br />
     var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
     for (i = 0; i &lt; jsonObject.length; i++) {<br />
         html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
     }<br />
     html += ‘&lt;/table&gt;’;<br />
     $(‘#initialData’).append(html);<br />
 }<br />
[/js]</p>
<p>What we are doing here is parsing our data and appending the data to our UI element using a for loop.</p>
<p>Now after running it, you will get the following output.</p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-objects-with-same-property-and-separate-them/Images/get%20output.jpg" alt="" /></p>
<p>We have successfully formatted our data and shown it to our UI. Cool, right?</p>
<p>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:</p>
<p>[js]<br />
$(‘#separateme’).click(function () {<br />
         loadnewData();<br />
         $(‘#separateme’).hide();<br />
         $(‘#newData’).show();<br />
     });<br />
[/js]</p>
<p>Here is the definition of the loadnewData() function.</p>
<p>[js]<br />
function loadnewData() {<br />
      for (i = 0; i &lt; uniqueNames.length; i++) {<br />
          var currentName = uniqueNames[i];<br />
          var uniqueDataArray = $.grep(jsonObject, function (data) {<br />
              return data.name == currentName;<br />
          });<br />
          var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
          for (j = 0; j &lt; uniqueDataArray.length; j++) {<br />
              html += ‘&lt;tr&gt;&lt;td&gt;’ + uniqueDataArray[j].name + ‘&lt;/td&gt;&lt;td&gt;’ + uniqueDataArray[j].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + uniqueDataArray[j].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
          }<br />
          html += ‘&lt;/table&gt;’;<br />
          $(‘#newData’).append(html);<br />
      }<br />
  }<br />
[/js]</p>
<p>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.</p>
<p>[js]<br />
var uniqueDataArray = $.grep(jsonObject, function (data) {<br />
        return data.name == currentName;<br />
        });<br />
[/js]<br />
What our “grep” function does is, it will take all objects at a time that has the property “name” as currentName.<br />
[js]<br />
return data.name == currentName;<br />
[/js]</p>
<p>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.</p>
<p><strong>Complete Code</strong></p>
<p>[html]<br />
&lt;!DOCTYPE html&gt;<br />
&lt;html&gt;<br />
&lt;head&gt;<br />
    &lt;title&gt;Find JSON Object by its property,separate and show demo – Sibeesh Passion&lt;/title&gt;<br />
    &lt;script src=“jquery-2.1.4.min.js”&gt;&lt;/script&gt;<br />
    &lt;style&gt;<br />
        #initialData {<br />
            border: 1px solid #999;<br />
            width:220px;<br />
            padding:10px;<br />
            float:left;<br />
        }<br />
        #newData {<br />
            border: 1px solid #999;<br />
            width:220px;<br />
            padding:10px;<br />
            float:left;<br />
        }<br />
         td {<br />
            border: 1px solid #ccc;<br />
            padding: 5px;<br />
            text-align: center;<br />
        }<br />
    &lt;/style&gt;<br />
    &lt;script&gt;<br />
        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}]}]’;<br />
        var jsonObject;<br />
        var uniqueNames = [2013, 2012, 2014];<br />
        $(function () {<br />
            $(‘#newData’).hide();<br />
            loadinitialData();<br />
            $(‘#separateme’).click(function () {<br />
                loadnewData();<br />
                $(‘#separateme’).hide();<br />
                $(‘#newData’).show();<br />
            });<br />
        });<br />
        function loadinitialData() {<br />
            jsonObject = $.parseJSON(data);<br />
            var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
            for (i = 0; i &lt; jsonObject.length; i++) {<br />
                html += ‘&lt;tr&gt;&lt;td&gt;’ + jsonObject[i].name + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + jsonObject[i].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
            }<br />
            html += ‘&lt;/table&gt;’;<br />
            $(‘#initialData’).append(html);<br />
        }<br />
        function loadnewData() {<br />
            for (i = 0; i &lt; uniqueNames.length; i++) {<br />
                var currentName = uniqueNames[i];<br />
                var uniqueDataArray = $.grep(jsonObject, function (data) {<br />
                    return data.name == currentName;<br />
                });<br />
                var html = ‘&lt;table&gt;&lt;th&gt;Year&lt;/th&gt;&lt;th&gt;X Value&lt;/th&gt;&lt;th&gt;Y Value&lt;/th&gt;’;<br />
                for (j = 0; j &lt; uniqueDataArray.length; j++) {<br />
                    html += ‘&lt;tr&gt;&lt;td&gt;’ + uniqueDataArray[j].name + ‘&lt;/td&gt;&lt;td&gt;’ + uniqueDataArray[j].data[0].x + ‘&lt;/td&gt;&lt;td&gt;’ + uniqueDataArray[j].data[0].y + ‘&lt;/td&gt;&lt;/tr&gt;’;<br />
                }<br />
                html += ‘&lt;/table&gt;’;<br />
                $(‘#newData’).append(html);<br />
            }<br />
        }<br />
    &lt;/script&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
    &lt;div id=“initialData”&gt;&lt;/div&gt;<br />
    &lt;div id=“newData”&gt;&lt;/div&gt;<br />
    &lt;button id=“separateme”&gt;Make Me Separate&lt;/button&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
[/html]<br />
Now it is time to see our output as in the following.</p>
<p><strong>output</strong></p>
<p><img decoding="async" src="http://www.c-sharpcorner.com/UploadFile/65794e/find-objects-with-same-property-and-separate-them/Images/output.jpg" alt="" /></p>
<p>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.</p>
<p><strong>Conclusion</strong></p>
<p>I hope you liked this article. Please share with me your valuable thoughts and comments. Your feedback is always welcomed.<br />
Thanks in advance. Happy coding!</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.sibeeshpassion.com/find-json-objects-with-same-property-and-separate-them/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
