
- 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
Count the number of data types in an array - JavaScript
We are required to write a JavaScript function that takes in an array that contains elements of different data types and the function should return a map representing the frequency of each data type.
Let’s say the following is our array −
const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh' }, [2, 4, 7], 'dfd', null, Symbol('*'), 8];
Example
Following is the code −
const arr = [23, 'df', undefined, null, 12, { name: 'Rajesh'}, [2, 4, 7], 'dfd', null, Symbol('*'), 8]; const countDataTypes = arr => { return arr.reduce((acc, val) => { const dataType = typeof val; if(acc.has(dataType)){ acc.set(dataType, acc.get(dataType)+1); }else{ acc.set(dataType, 1); }; return acc; }, new Map()); }; console.log(countDataTypes(arr));
Output
Following is the output in the console −
Map(5) { 'number' => 3, 'string' => 2, 'undefined' => 1, 'object' => 4, 'symbol' => 1 }
- Related Articles
- JavaScript Count the number of unique elements in an array of objects by an object property?
- How to count the number of elements in an array below/above a given number (JavaScript)
- How to count number of occurrences of repeated names in an array - JavaScript?
- Count the number of items in an array in MongoDB?
- Count number of primes in an array in C++
- Count number of elements in an array with MongoDB?
- Deep count of elements of an array using JavaScript
- C# program to count number of bytes in an array
- How to count the number of duplicate rows in an R data frame?
- Number of vowels within an array in JavaScript
- What is the concept of Data Types in JavaScript?
- Get the closest number out of an array in JavaScript
- Count number of even and odd elements in an array in C++
- Returning the value of (count of positive / sum of negatives) for an array in JavaScript
- Upper or lower elements count in an array in JavaScript

Advertisements