- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Product of numbers present in a nested array in JavaScript
We are required to write a JavaScript function that takes in an array of nested arrays of Numbers and some falsy values (including 0) and some strings as well and the function should return the product of number values present in the nested array. If the array contains some 0s, we should ignore them as well.
Example
The code for this will be −
const arr = [ 1, 2, null, [ 2, 5, null, undefined, false, 5, [ 1, 3, false, 0, 2 ], 4, false ], 4, 6, 0 ]; const recursiveMultiplication = arr => { let prod = 1; for(let i = 0; i < arr.length; i++){ if(Array.isArray(arr[i])){ prod *= recursiveMultiplication(arr[i]); }else{ prod *= arr[i] || 1; }; }; return prod; }; console.log(recursiveMultiplication(arr));
Output
The output in the console −
57600
- Related Articles
- JavaScript - summing numbers from strings nested in array
- Product of all other numbers an array in JavaScript
- Detecting the largest element in an array of Numbers (nested) in JavaScript
- Difference between numbers and string numbers present in an array in JavaScript
- Weight sum of a nested array in JavaScript
- Grouping nested array in JavaScript
- Flattening a deeply nested array of literals in JavaScript
- Join in nested array in JavaScript
- Finding the maximum in a nested array - JavaScript
- Recursion - Sum Nested Array in JavaScript
- Return the greatest possible product of n numbers from the array in JavaScript
- Finding a greatest number in a nested array in JavaScript
- Simplifying nested array JavaScript
- Sum of nested object values in Array using JavaScript
- Maximum Product of Two Numbers in a List of Integers in JavaScript

Advertisements