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 }));

更多相关文章

  1. 对多维数组中的列进行排序
  2. 获取拆分字符串数组的最后一个元素
  3. JS将字符串转换为数组
  4. 将textarea值附加到现有数组javascript
  5. ECMAScript6(6):数组的扩展
  6. 希望日期开始结束在一个数组中的while循环中为一个房间ID
  7. 如何使用客户端Javascript数组并通过节点发布。将js API插入Mong
  8. 在量角器中检索子元素的数组
  9. 从特定条件下存储在localStorage中的数组中删除对象?

随机推荐

  1. JQuery实现的 checkbox 全选、反选。
  2. 执行Django数据库值的计算:视图。py或Jav
  3. 通过AJAX加载内容和预加载图像?
  4. 使用JQuery从外部文件中通过id选择器获取
  5. 使用jQuery更新textarea值更改的文本
  6. 是否每次都可能分离内容、表示和行为?
  7. jquery向Django后台发送数组
  8. 将JavaScript对象作为Dictionary 传递给C
  9. 如何防止缓存.NET JSON源
  10. 如何在用户选择操作后获取当前日期和时间