

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Reduce array in JavaScript
Suppose, we have an array of objects like this −
const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ];
We are required to write a JavaScript function that takes in one such array and does the following −
Extract the times from the json code: so: 18:00:00, 10:00:00, 16:30:00
Convert the times to this: [18,0], [10,0], [16,30]
Put it in an array.
Return the final array.
Example
The code for this will be −
const arr = [ {"time":"18:00:00"}, {"time":"10:00:00"}, {"time":"16:30:00"} ]; const reduceArray = (arr = []) => { let res = []; res = arr.map(obj => { return obj['time'].split(':').slice(0, 2).map(el => { return +el; }); }); return res; }; console.log(reduceArray(arr));
Output
And the output in the console will be −
[ [ 18, 0 ], [ 10, 0 ], [ 16, 30 ] ]
- Related Questions & Answers
- Sorting Array with JavaScript reduce function - JavaScript
- Reduce an array to groups in JavaScript
- JavaScript reduce sum array with undefined values
- Finding the product of array elements with reduce() in JavaScript
- Convert 2D array to object using map or reduce in JavaScript
- Retaining array elements greater than cumulative sum using reduce() in JavaScript
- How to convert array into array of objects using map() and reduce() in JavaScript
- How to reduce arrays in JavaScript?
- Comparing forEach() and reduce() for summing an array of numbers in JavaScript.
- Reduce an array to the sum of every nth element - JavaScript
- Reduce a multi-dimensional array in Numpy
- Average with the Reduce Method in JavaScript
- Reduce Array Size to The Half in C++
- Encoding string to reduce its size in JavaScript
- Reduce array's dimension by one in Numpy
Advertisements