
- 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
Grouping on the basis of object property 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 −
Example
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);
Output
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
- Array grouping on the basis of children object’s property in 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?
- Define grouping of Object
- Explain the basis for grouping organisms into five kingdoms.
- Determining rank on basis of marks in JavaScript
- Grouping names based on first letter in JavaScript
- How to sort a JavaScript object list based on a property when the property is not consistent
- CSS flex-basis property
- How to check if every property on object is the same recursively in JavaScript?
- Enter values with prompt and evaluate on the basis of conditions in JavaScript?
- Complicated array grouping JavaScript
- Replacing array of object property name in JavaScript
- map() array of object titles into a new array based on other property value JavaScript

Advertisements