What is the best way to get the array with the max length?
I have an array as follows,
main = [
[1,2,3],
[4],
[5,6,7,8],
[9,0]
]
I need to store the array with the max length, in this case
maxArray = main[2]
Use Array#reduce method.
main = [
[1, 2, 3],
[4],
[5, 6, 7, 8],
[9, 0]
];
console.log(
main.reduce(function(a, b) {
return a.length > b.length ? a : b;
})
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With