查找数组中最长的string
有一个简短的方法来查找string数组中最长的string?
像arr.Max(x => x.Length);
?
var longest = arr.sort(function (a, b) { return b.length - a.length; })[0];
可能效率更高,但只有自Javascript 1.8 / ECMAScript 5才可用,并且在旧版浏览器中默认不可用:
var longest = arr.reduce(function (a, b) { return a.length > b.length ? a : b; });
我会做这样的事情
var arr = ['first item', 'second item is longer than the third one', 'third longish item']; var lgth = 0; var longest; for(var i=0; i < arr.length; i++){ if(arr[i].length > lgth){ var lgth = arr[i].length; longest = arr[i]; } } alert(longest);
一个老问题的新答案:在ES6中,你可以缩短:
Math.max(...(x.map(el => el.length)));
使用Array.prototype – (sorting类似于我在做小提琴时由@katsPaugh和@deceze发布的内容)
在这里演示
var arr = [ "2 --", "3 ---", "4 ----", "1 -", "5 -----" ]; Array.prototype.longest=function() { return this.sort( function(a,b) { if (a.length > b.length) return -1; if (a.length < b.length) return 1; return 0 } )[0]; } alert(arr.longest());
我看到最短的解决scheme
function findLong(s){ return Math.max.apply(null, s.split(' ').map(w => w.length)); }
var arr = [ 'fdgdfgdfg', 'gdfgf', 'gdfgdfhawsdgd', 'gdf', 'gdfhdfhjurvweadsd' ]; arr.sort(function (a, b) { return b.length - a.length })[0];
我会做这样的事情:
function findLongestWord(str) { var array = str.split(" "); var maxLength=array[0].length; for(var i=0; i < array.length; i++ ) { if(array[i].length > maxLength) maxLength = array[i].length} return maxLength;} findLongestWord("What if we try a super-long word such as otorhinolaryngology");
也许不是最快的,但肯定是非常可读的:
function findLongestWord(array) { var longestWord = ""; array.forEach(function(word) { if(word.length > longestString.length) { longestWord = word; } }); return longestWord; } var word = findLongestWord(["The","quick","brown", "fox", "jumped", "over", "the", "lazy", "dog"]); console.log(word); // result is "jumped"
自IE9 +以来,支持数组函数forEach 。
var longest = (arr) => { let sum = 0 arr.map((e) => { sum = e.length > sum ? e.length : sum }) return sum }
它可以是工作
function findLongestWord(str) { str = str.split(" "); var sorted = str.sort(function(prev,current){ return prev.length - current.length; }); var index = sorted.length; str = sorted[index-1]; return str; } findLongestWord("The quick brown fox jumped over the lazy dog");
如果你的string已经被分割成一个数组,你将不需要拆分部分。
function findLongestWord(str) { str = str.split(' '); var longest = 0; for(var i = 0; i < str.length; i++) { if(str[i].length >= longest) { longest = str[i].length; } } return longest; } findLongestWord("The quick brown fox jumped over the lazy dog");
我对Jason的function有了一些启发,并对它做了一些改进,结果得到了相当快的发现者:
function timo_longest(a) { var c = 0, d = 0, l = 0, i = a.length; if (i) while (i--) { d = a[i].length; if (d > c) { l = i; c = d; } } return a[l]; } arr=["First", "Second", "Third"]; var longest = timo_longest(arr);
速度结果: http : //jsperf.com/longest-string-in-array/7
var array = ["hello","falsey","undefined"]; var findLongestWord = function(array){ var longest = array.reduce(function(a,b){ return (a.length > b.length) ? a : b; }); return longest; } findLongestWord(array);
这是5分钟前我写的非常简单的bug代码。 我没有时间来修复错误,但如果你有这个想法,你可以改变和使用它。
Array.prototype.MaxBy = function(fn) { var max = 0; var element; for (var i = 0; i < this.length; i++) { var ret = fn(this[i]); if (ret > max) { max = ret; element = this[i]; } } return element; }; function showOldestPerson() { var array = [{ Name: "cihat", Age: 28 }, { Name: "Ali", Age: 30 }, { Name: "Kutlu", Age: 27}]; var person = array.MaxBy(function(item) { return item.Age; }); alert(person.Name); }