
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
JavaScript group a JSON object by two properties and count
Suppose, we have an array of objects like this −
const arr = [ {"location":"Kirrawee","identity_long":"student"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"worker"}, {"location":"Sutherland","identity_long":"student"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Sutherland","identity_long":"worker"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Miranda","identity_long":"resident"}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"student"}, {"location":"Miranda","identity_long":""}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"resident"} ];
We are required to write a JavaScript function that takes in one such array of objects. The function should prepare a new array of objects in which all (identical) the objects are grouped together based on the location property.
And the objects should be assigned a count property that contains the number of times it appeared in the original array of objects.
Therefore, for the above array, the output should look like −
const output = [ {"location":"Kirrawee","identity":"student","count":1}, {"location":"Kirrawee","identity":"visitor","count":2}, {"location":"Kirrawee","identity":"worker","count":1}, {"location":"Sutherland","identity":"student","count":1}, {"location":"Sutherland","identity":"resident","count":2}, {"location":"Sutherland","identity":"worker","count":1}, {"location":"Miranda","identity":"resident","count":2}, {"location":"Miranda","identity":"worker","count":2}, {"location":"Miranda","identity":"student","count":1} ];
Example
The code for this will be −
const arr = [ {"location":"Kirrawee","identity_long":"student"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"visitor"}, {"location":"Kirrawee","identity_long":"worker"}, {"location":"Sutherland","identity_long":"student"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Sutherland","identity_long":"worker"}, {"location":"Sutherland","identity_long":"resident"}, {"location":"Miranda","identity_long":"resident"}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"student"}, {"location":"Miranda","identity_long":""}, {"location":"Miranda","identity_long":"worker"}, {"location":"Miranda","identity_long":"resident"} ]; const groupArray = (arr = []) => { // create map let map = new Map() for (let i = 0; i < arr.length; i++) { const s = JSON.stringify(arr[i]); if (!map.has(s)) { map.set(s, { location: arr[i].location, identity: arr[i].identity_long, count: 1, }); } else { map.get(s).count++; } } const res = Array.from(map.values()) return res; }; console.log(groupArray(arr));
Output
And the output in the console will be −
[ { location: 'Kirrawee', identity: 'student', count: 1 }, { location: 'Kirrawee', identity: 'visitor', count: 2 }, { location: 'Kirrawee', identity: 'worker', count: 1 }, { location: 'Sutherland', identity: 'student', count: 1 }, { location: 'Sutherland', identity: 'resident', count: 2 }, { location: 'Sutherland', identity: 'worker', count: 1 }, { location: 'Miranda', identity: 'resident', count: 2 }, { location: 'Miranda', identity: 'worker', count: 2 }, { location: 'Miranda', identity: 'student', count: 1 }, { location: 'Miranda', identity: '', count: 1 } ]
- Related Articles
- JSON group object in JavaScript
- Merge and group object properties in JavaScript
- Group values in array by two properties JavaScript
- Group by JavaScript Array Object
- Converting two arrays into a JSON object in JavaScript
- Using group by on two fields and count in MySQL?
- Search by id and remove object from JSON array in JavaScript
- Sorting JavaScript object by length of array properties.
- JavaScript Object Properties
- MySQL- GROUP and COUNT by date?
- Sorting a JSON object in JavaScript
- Flattening a JSON object in JavaScript
- Converting array to object by splitting the properties - JavaScript
- Constructing a nested JSON object in JavaScript
- How to convert bean to JSON object by excluding some properties using JsonConfig in Java?

Advertisements