发现元素的数组数最高 [英] Finding the highest number of elements in arrays
本文介绍了发现元素的数组数最高的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想找到最长的阵列,并得到他的长度。
这是数组:
[667653905,Johjn,史密斯],[500500500,约翰,Smith2,另一场],[12342312 ASD,asdasd,fghfgh,fghfgh]]
目前我使用的循环,但这是很慢的解决方案,如果我有50K的数组:
为(VAR I = 0; I< explode.length;我++){
的console.log(爆炸[I]。长度);
}
解决方案
您可以找到最长阵列。降低()
VAR最长= theArray.reduce(函数(LSF,A){
回报!LSF ||则为a.length> lsf.length?答:LSF;
}, 空值);
的console.log(最长的数组有+ longest.length +元素);
I want to find the longest array and get his length.
This is array:
[["667653905", "Johjn", "Smith"], ["500500500", "John", "Smith2", "Another field"], ["12342312", "asd", "asdasd", "fghfgh", "fghfgh"]]
Currently i am using loop but this is very slow solution if i have 50k arrays:
for(var i = 0; i<explode.length; i++) {
console.log(explode[i].length);
}
解决方案
You can find the longest array with .reduce()
var longest = theArray.reduce(function(lsf, a) {
return !lsf || a.length > lsf.length ? a : lsf;
}, null);
console.log("longest array has " + longest.length + " elements");
这篇关于发现元素的数组数最高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文