
- 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
How do I write a function that takes an array of values and returns an object JavaScript?
Let’s say, we are required to write a function classifyArray() that takes in an array which contains mixed data types and returns a Map() with the elements grouped by their data types.
For example −
// if the input array is: const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; // then the output Map should be: Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ], 'symbol' => [ Symbol(foo) ], 'boolean' => [ true, false ] }
Now let’s write the code for this function −
Example
const arr = ['class', 2, [7, 8, 9], {"name": "Michael"}, Symbol('foo'), true, false, 'name', 6]; const classifyArray = arr => { return arr.reduce((acc, val) => { const previousData = acc.get(typeof val); if(previousData){ acc.set(typeof val, [...previousData, val]); }else{ acc.set(typeof val, [val]); }; return acc; }, new Map()); }; console.log(classifyArray(arr));
Output
The output in the console will be −
Map(5) { 'string' => [ 'class', 'name' ], 'number' => [ 2, 6 ], 'object' => [ [ 7, 8, 9 ], { name: 'Michael' } ], 'symbol' => [ Symbol(foo) ], 'boolean' => [ true, false ] }
- Related Articles
- Function that returns the minimum and maximum value of an array in JavaScript
- How do I check if an array includes an object in JavaScript?
- How to make a function that returns the factorial of each integer in an array JavaScript
- Write an algorithm that takes an array and moves all of the zeros to the end JavaScript
- Converting a JavaScript object to an array of values - JavaScript
- How to edit values of an object inside an array in a class - JavaScript?
- How do I empty an array in JavaScript?
- How do we check if an object is an array in Javascript?
- Retrieve key and values from object in an array JavaScript
- How to merge an array with an object where values are arrays - JavaScript
- How to use array that include and check an object against a property of an object?
- How to call a function that returns another function in JavaScript?
- How to remove elements from an array until the passed function returns true in JavaScript?
- How can I convert an array to an object by splitting strings? JavaScript
- How do I remove a particular element from an array in JavaScript

Advertisements