如果我有以下数组:
array = ['a', 'abcde', 'ab'] ;
我想获得数组元素的最大长度,即 5(对于元素“abcde”)。
我知道您可以通过(例如)array[1].length 获取数组元素的长度,但我不知道如何获取最大长度。
任何使用 JavaScript 或 jQuery 的答案都很好。
TIA
保罗·琼斯
最佳答案
一条线给你:
Math.max.apply(Math, $.map(array, function (el) { return el.length }));
工作示例:http://jsfiddle.net/5SDBx/
您也可以在较新的浏览器中不使用 jQuery(甚至是带有 Array.prototype.map
的 compatibility implementation 的旧浏览器):
Math.max.apply(Math, array.map(function (el) { return el.length }));
关于javascript - 获取 JavaScript 数组元素的最大长度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6043471/