How Do I Find the Largest Number in a 3D JavaScript Array?


Let’s say the following is our array;

var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]];

Use the concept of flat() within the Math.max() to get the largest number.

Example

var theValuesIn3DArray = [75, [18, 89], [56, [97], [99]]];
Array.prototype.findTheLargestNumberIn3dArray = function (){
   return Math.max(...this.flat(Infinity));
}
console.log("The largest number in 3D array is=");
console.log(theValuesIn3DArray.findTheLargestNumberIn3dArray());

To run the above program, you need to use the following command −

node fileName.js.

Here, my file name is demo202.js.

Output

This will produce the following output −

PS C:\Users\Amit\javascript-code> node demo202.js
The largest number in 3D array is=
99

Updated on: 14-Sep-2020

119 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements