let a = [
[1, 2, 3, [1, 233]],
[33, 45, 65, 4, 111, 123],
[34, 5, 6]
];
let max = a[0][1];
for (let i = 0; i < a.length; i++) {
for (let j = 0; j < a[i].length; j++) {
if (a[i][j] > max) {
max = a[i][j];
for (let k = 0; k < a[i][j].length; k++) {
if (a[i][j][k] > max) {
max = a[i][j][k];
}
}
}
}
}
console.log(max);
Asked
Active
Viewed 42 times
0

mplungjan
- 169,008
- 28
- 173
- 236

ANKIT CHHAYA
- 1
- 1
-
`Math.max(...a.flat(Infinity))` – Bravo Aug 15 '21 at 06:55
1 Answers
3
You can flatten the array and use Math.max()
to get the max.
> Math.max(...a.flat(Infinity))
233

Davo
- 526
- 3
- 19
-
1`Infinity` will be bigger than any array depth - as the argument to flat is a limit, `Infinity` won't effect performance - it's not like the code will loop infinitely :p – Bravo Aug 15 '21 at 06:59
-
-
1
-