
- 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
Return a map representing the frequency of each data type in an array in 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 to return a map representing the frequency of each datatype −
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
The output in the console will be −
Map(5) { 'number' => 3, 'string' => 2, 'undefined' => 1, 'object' => 4, 'symbol' => 1 }
- Related Articles
- Python - Return an array representing the data in the Pandas Index
- Return the maximum number in each array using map JavaScript
- Building frequency map of all the elements in an array JavaScript
- Remove duplicates and map an array in JavaScript
- Return the data of a masked array as an ndarray
- Write a Golang program to find the frequency of each element in an array
- From an array of arrays, return an array where each item is the sum of all the items in the corresponding subarray in JavaScript
- How to find the frequency of each value in an R data frame?
- Find the minimal data type of an array-like in Python
- Return an array of ones with the same shape and type as a given array in Numpy
- Return an array of zeros with the same shape and type as a given array in Numpy
- Finding average in mixed data type array in JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Find the frequency of unique values for each column in an R data frame.
- Return indexes of greatest values in an array in JavaScript

Advertisements