获取JavaScript数组元素的最大长度
If I have the following array :
如果我有以下数组:
array = ['a', 'abcde', 'ab'] ;
I would like to get the maximum length of the array elements ie 5 (for the element 'abcde').
我想获得数组元素的最大长度,即5(对于元素'abcde')。
I know that you can get the length of an array element via (eg) array[1].length but I don't know how to get the maximum length.
我知道你可以通过(例如)array [1] .length获取数组元素的长度,但我不知道如何获得最大长度。
Any answer in JavaScript or jQuery would be great.
JavaScript或jQuery中的任何答案都会很棒。
TIA
TIA
Paul Jones
保罗琼斯
7 个解决方案
#1
20
One-liner for you:
单行为您:
Math.max.apply(Math, $.map(array, function (el) { return el.length }));
Working example: http://jsfiddle.net/5SDBx/
工作示例:http://jsfiddle.net/5SDBx/
You can do it without jQuery in newer browsers (or even older browsers with a compatibility implementation of Array.prototype.map
) too:
你可以在没有jQuery的新浏览器(甚至是具有Array.prototype.map兼容性实现的旧浏览器)中实现它:
Math.max.apply(Math, array.map(function (el) { return el.length }));
更多相关文章
- 对多维数组中的列进行排序
- 获取拆分字符串数组的最后一个元素
- JS将字符串转换为数组
- 将textarea值附加到现有数组javascript
- ECMAScript6(6):数组的扩展
- 希望日期开始结束在一个数组中的while循环中为一个房间ID
- 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong
- 在量角器中检索子元素的数组
- 从特定条件下存储在localStorage中的数组中删除对象?