Sibeesh Passion

Top Menu

  • Home
  • Search
  • About
  • Privacy Policy

Main Menu

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Home
  • Search
  • About
  • Privacy Policy

logo

Sibeesh Passion

  • Articles
    • Azure
    • .NET
    • IoT
    • JavaScript
    • Career Advice
    • Interview
    • Angular
    • Node JS
    • JQuery
    • Knockout JS
    • Jasmine Framework
    • SQL
    • MongoDB
    • MySQL
    • WordPress
  • Contributions
    • Medium
    • GitHub
    • Stack Overflow
    • Unsplash
    • ASP.NET Forum
    • C# Corner
    • Code Project
    • DZone
    • MSDN
  • Social Media
    • LinkedIn
    • Facebook
    • Instagram
    • Twitter
  • YouTube
    • Sibeesh Venu
    • Sibeesh Passion
  • Awards
  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment

  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins

  • Post Messages to Microsoft Teams Using Python

  • Get Azure Blob Storage Blob Metadata Using PowerShell

  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines

JQueryJson
Home›JQuery›Find JSON Objects With Same Property and Separate Them

Find JSON Objects With Same Property and Separate Them

By SibeeshVenu
May 29, 2015
979
0
Share:

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]
<script src=“jquery-2.1.4.min.js”></script>
[/js]

Now we need some data, right? Please have a look at the following JSON data.

[js]
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}]}]’;
[/js]

Next we need some UI elements as in the following:

[html]
<div id=“initialData”></div>
<div id=“newData”></div>
<button id=“separateme”>Make Me Separate</button>
[/html]

What next? We need to show this data to our UI, right? For that I am calling a function in our document ready function.

[js]
$(function () {
$(‘#newData’).hide();
loadinitialData();
});
[/js]

The following is the function definition for the loadinitialData() function.

[js]
function loadinitialData() {
jsonObject = $.parseJSON(data);
var html = ‘<table><th>Year</th><th>X Value</th><th>Y Value</th>’;
for (i = 0; i < jsonObject.length; i++) {
html += ‘<tr><td>’ + jsonObject[i].name + ‘</td><td>’ + jsonObject[i].data[0].x + ‘</td><td>’ + jsonObject[i].data[0].y + ‘</td></tr>’;
}
html += ‘</table>’;
$(‘#initialData’).append(html);
}
[/js]

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.

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:

[js]
$(‘#separateme’).click(function () {
loadnewData();
$(‘#separateme’).hide();
$(‘#newData’).show();
});
[/js]

Here is the definition of the loadnewData() function.

[js]
function loadnewData() {
for (i = 0; i < uniqueNames.length; i++) {
var currentName = uniqueNames[i];
var uniqueDataArray = $.grep(jsonObject, function (data) {
return data.name == currentName;
});
var html = ‘<table><th>Year</th><th>X Value</th><th>Y Value</th>’;
for (j = 0; j < uniqueDataArray.length; j++) {
html += ‘<tr><td>’ + uniqueDataArray[j].name + ‘</td><td>’ + uniqueDataArray[j].data[0].x + ‘</td><td>’ + uniqueDataArray[j].data[0].y + ‘</td></tr>’;
}
html += ‘</table>’;
$(‘#newData’).append(html);
}
}
[/js]

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.

[js]
var uniqueDataArray = $.grep(jsonObject, function (data) {
return data.name == currentName;
});
[/js]
What our “grep” function does is, it will take all objects at a time that has the property “name” as currentName.
[js]
return data.name == currentName;
[/js]

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

[html]
<!DOCTYPE html>
<html>
<head>
<title>Find JSON Object by its property,separate and show demo – Sibeesh Passion</title>
<script src=“jquery-2.1.4.min.js”></script>
<style>
#initialData {
border: 1px solid #999;
width:220px;
padding:10px;
float:left;
}
#newData {
border: 1px solid #999;
width:220px;
padding:10px;
float:left;
}
td {
border: 1px solid #ccc;
padding: 5px;
text-align: center;
}
</style>
<script>
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}]}]’;
var jsonObject;
var uniqueNames = [2013, 2012, 2014];
$(function () {
$(‘#newData’).hide();
loadinitialData();
$(‘#separateme’).click(function () {
loadnewData();
$(‘#separateme’).hide();
$(‘#newData’).show();
});
});
function loadinitialData() {
jsonObject = $.parseJSON(data);
var html = ‘<table><th>Year</th><th>X Value</th><th>Y Value</th>’;
for (i = 0; i < jsonObject.length; i++) {
html += ‘<tr><td>’ + jsonObject[i].name + ‘</td><td>’ + jsonObject[i].data[0].x + ‘</td><td>’ + jsonObject[i].data[0].y + ‘</td></tr>’;
}
html += ‘</table>’;
$(‘#initialData’).append(html);
}
function loadnewData() {
for (i = 0; i < uniqueNames.length; i++) {
var currentName = uniqueNames[i];
var uniqueDataArray = $.grep(jsonObject, function (data) {
return data.name == currentName;
});
var html = ‘<table><th>Year</th><th>X Value</th><th>Y Value</th>’;
for (j = 0; j < uniqueDataArray.length; j++) {
html += ‘<tr><td>’ + uniqueDataArray[j].name + ‘</td><td>’ + uniqueDataArray[j].data[0].x + ‘</td><td>’ + uniqueDataArray[j].data[0].y + ‘</td></tr>’;
}
html += ‘</table>’;
$(‘#newData’).append(html);
}
}
</script>
</head>
<body>
<div id=“initialData”></div>
<div id=“newData”></div>
<button id=“separateme”>Make Me Separate</button>
</body>
</html>
[/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.
Thanks in advance. Happy coding!

TagsJQueryJsonJSON ObjectObject By Property
Previous Article

Solutions For Wamp Server Not Starting Issue

Next Article

Move Elements on Mouse Click Using jQuery

0
Shares
  • 0
  • +
  • 0
  • 0
  • 0

SibeeshVenu

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer.

Related articles More from author

  • Code SnippetsJQuery

    How to reload the page when resize the page in jQuery

    May 31, 2015
    By SibeeshVenu
  • HighChart

    Working With Charts

    April 29, 2015
    By SibeeshVenu
  • Code SnippetsJQuery

    Delete an element from an array in jQuery using the index

    May 31, 2015
    By SibeeshVenu
  • Line Chart With Custom Tool Tip
    How toHTML5

    Client Side Chart Widget in HTML 5: Part 7 (Line Chart With Custom ToolTip)

    January 29, 2015
    By SibeeshVenu
  • .NETASP.NETC#JsonMicrosoft ADOMD

    How to Convert Microsoft ADOMD Data Source to JSON

    January 29, 2015
    By SibeeshVenu
  • CodeProjectJQuery

    Find And Exclude Element From Array

    August 5, 2015
    By SibeeshVenu
0

My book

Asp Net Core and Azure with Raspberry Pi Sibeesh Venu

YouTube

MICROSOFT MVP (2016-2022)

profile for Sibeesh Venu - Microsoft MVP

Recent Posts

  • Linux Azure Function Isolated Dot Net 9 YAML Template Deployment
  • Build, Deploy, Configure CI &CD Your Static Website in 5 mins
  • Easily move data from one COSMOS DB to another
  • .NET 8 New and Efficient Way to Check IP is in Given IP Range
  • Async Client IP safelist for Dot NET
  • Post Messages to Microsoft Teams Using Python
  • Get Azure Blob Storage Blob Metadata Using PowerShell
  • Deploy .net 6 App to Azure from Azure DevOps using Pipelines
  • Integrate Azure App Insights in 1 Minute to .Net6 Application
  • Azure DevOps Service Connection with Multiple Azure Resource Group

Tags

Achievements (35) Angular (14) Angular 5 (7) Angular JS (15) article (10) Article Of The Day (13) Asp.Net (14) Azure (65) Azure DevOps (10) Azure Function (10) Azure IoT (7) C# (17) c-sharp corner (13) Career Advice (11) chart (11) CSharp (7) CSS (7) CSS3 (6) HighChart (10) How To (9) HTML5 (10) HTML5 Chart (11) Interview (6) IoT (11) Javascript (10) JQuery (82) jquery functions (9) JQWidgets (15) JQX Grid (17) Json (7) Microsoft (8) MVC (20) MVP (9) MXChip (7) News (18) Office 365 (7) Products (10) SQL (20) SQL Server (15) Visual Studio (10) Visual Studio 2017 (7) VS2017 (7) Web API (12) Windows 10 (7) Wordpress (9)
  • .NET
  • Achievements
  • ADO.NET
  • Android
  • Angular
  • Arduino
  • Article Of The Day
  • ASP.NET
  • Asp.Net Core
  • Automobile
  • Awards
  • Azure
  • Azure CDN
  • azure devops
  • Blockchain
  • Blog
  • Browser
  • C-Sharp Corner
  • C#
  • Career Advice
  • Code Snippets
  • CodeProject
  • Cognitive Services
  • Cosmos DB
  • CSS
  • CSS3
  • Data Factory
  • Database
  • Docker
  • Drawings
  • Drill Down Chart
  • English
  • Excel Programming
  • Exporting
  • Facebook
  • Fun
  • Gadgets
  • GitHub
  • GoPro
  • High Map
  • HighChart
  • How to
  • HTML
  • HTML5
  • Ignite UI
  • IIS
  • Interview
  • IoT
  • JavaScript
  • JQuery
  • jQuery UI
  • JQWidgets
  • JQX Grid
  • Json
  • Knockout JS
  • Linux
  • Machine Learning
  • Malayalam
  • Malayalam Poems
  • MDX Query
  • Microsoft
  • Microsoft ADOMD
  • Microsoft MVP
  • Microsoft Office
  • Microsoft Technologies
  • Microsoft Windows
  • Microsoft Windows Server
  • Mobile
  • MongoDB
  • Monthly Winners
  • MVC
  • MVC Grid
  • MySQL
  • News
  • Node JS
  • npm
  • Number Conversions
  • October 2015
  • Office 365
  • Office Development
  • One Plus
  • Outlook
  • Page
  • PHP
  • Poems
  • PowerShell
  • Products
  • Q&A
  • Raspberry PI
  • React
  • SEO
  • SharePoint
  • Skype
  • Social Media
  • Software
  • Spire.Doc
  • Spire.PDF
  • Spire.XLS
  • SQL
  • SQL Server
  • SSAS
  • SSMS
  • Storage In HTML5
  • Stories
  • Third Party Software Apps
  • Tips
  • Tools
  • Translator Text
  • Uncategorized
  • Unit Testing
  • UWP
  • VB.Net
  • Videos
  • Virtual Machine
  • Visual Studio
  • Visual Studio 2017
  • Wamp Server
  • Web API
  • Web Platform Installer
  • Webinars
  • WebMatrix
  • Windows 10
  • Windows 7
  • Windows 8.1
  • Wordpress
  • Writing

ABOUT ME

I am Sibeesh Venu, an engineer by profession and writer by passion. Microsoft MVP, Author, Speaker, Content Creator, Youtuber, Programmer. If you would like to know more about me, you can read my story here.

Contact Me

  • info@sibeeshpassion.com

Pages

  • About
  • Search
  • Privacy Policy
  • About
  • Search
  • Privacy Policy
© Copyright Sibeesh Passion 2014-2025. All Rights Reserved.
Go to mobile version