
- 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
Array grouping on the basis of children object’s property in JavaScript
We have an array of objects that contains data about some cars. The array is given as follows −
const cars = [{ company: 'Honda', type: 'SUV' }, { company: 'Hyundai', type: 'Sedan' }, { company: 'Suzuki', type: 'Sedan' }, { company: 'Audi', type: 'Coupe' }, { company: 'Tata', type: 'SUV' }, { company: 'Morris Garage', type: 'Hatchback' }, { company: 'Honda', type: 'SUV' }, { company: 'Tata', type: 'Sedan' }, { company: 'Honda', type: 'Hatchback' }];
We are required to write a program that groups the object together so that all the objects having the same value for type property appear together.
We will simply sort the array according to the type property so that the objects get aligned in alphabetical order of the types property.
The full code for doing this will be −
const cars = [{ company: 'Honda', type: 'SUV' }, { company: 'Hyundai', type: 'Sedan' }, { company: 'Suzuki', type: 'Sedan' }, { company: 'Audi', type: 'Coupe' }, { company: 'Tata', type: 'SUV' }, { company: 'Morris Garage', type: 'Hatchback' }, { company: 'Honda', type: 'SUV' }, { company: 'Tata', type: 'Sedan' }, { company: 'Honda', type: 'Hatchback' }]; const sorter = (a, b) => { return a.type.toLowerCase() > b.type.toLowerCase() ? 1 : -1; } cars.sort(sorter); console.log(cars);
The output in the console will be −
[ { company: 'Audi', type: 'Coupe' }, { company: 'Honda', type: 'Hatchback' }, { company: 'Morris Garage', type: 'Hatchback' }, { company: 'Tata', type: 'Sedan' }, { company: 'Suzuki', type: 'Sedan' }, { company: 'Hyundai', type: 'Sedan' }, { company: 'Honda', type: 'SUV' }, { company: 'Tata', type: 'SUV' }, { company: 'Honda', type: 'SUV' } ]
- Related Articles
- Grouping on the basis of object property JavaScript
- Grouping array of array on the basis of elements in JavaScript
- Grouping objects based on key property in JavaScript
- Finding content of arrays on the basis of specific property in JavaScript?
- Grouping array values in JavaScript
- Grouping nested array in JavaScript
- Replacing array of object property name in JavaScript
- Complicated array grouping JavaScript
- map() array of object titles into a new array based on other property value JavaScript
- Sum of array object property values in new array of objects in JavaScript
- Grouping an Array and Counting items creating new array based on Groups in JavaScript
- Tag names of body element's children in JavaScript?
- Define grouping of Object
- Explain the basis for grouping organisms into five kingdoms.
- Grouping and sorting 2-D array in JavaScript

Advertisements