
- 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 Count the number of unique elements in an array of objects by an object property?
Suppose, we have the following array of objects that contains data about orders placed in a restaurant −
const orders = [ {table_id: 3, food_id: 5}, {table_id: 4, food_id: 2}, {table_id: 1, food_id: 6}, {table_id: 3, food_id: 4}, {table_id: 4, food_id: 6}, ];
We are required to write a JavaScript function that takes in one such array. Our function should count the number of unique table_id property in the array (i.e., the number of unique tables for which the orders are booked).
And the number of unique food_id property (i.e., the number of unique food dishes ordered.)
Example
const orders = [ {table_id: 3, food_id: 5}, {table_id: 4, food_id: 2}, {table_id: 1, food_id: 6}, {table_id: 3, food_id: 4}, {table_id: 4, food_id: 6}, ]; const countUniques = (orders = []) => { const tableObj = {}, foodObj = {}; orders.forEach(el => { tableObj[el.table_id] = null; foodObj[el.food_id] = null; }); const tableUniqueIDs = Object.keys(tableObj).length; const foodUniqueIDs = Object.keys(foodObj).length; return { tableUniqueIDs, foodUniqueIDs }; }; console.log(countUniques(orders));
Output
And the output in the console will be −
{ tableUniqueIDs: 3, foodUniqueIDs: 4 }
- Related Articles
- Extract unique objects by attribute from an array of objects in JavaScript
- Unique number of occurrences of elements in an array in JavaScript
- Sorting an array of objects by property values - JavaScript
- Splitting an object into an array of objects in JavaScript
- Converting array of objects to an object of objects in JavaScript
- Sorting an array of objects by an array JavaScript
- Converting array of objects to an object in JavaScript
- Find the number of times a value of an object property occurs in an array with JavaScript?
- How to return an array whose elements are the enumerable property values of an object in JavaScript?
- Counting unique elements in an array in JavaScript
- Count number of elements in an array with MongoDB?
- Sorting an array objects by property having null value in JavaScript
- Deep count of elements of an array using JavaScript
- Convert an array of objects into plain object in JavaScript
- Flat a JavaScript array of objects into an object

Advertisements