javascript 查找数组中最长的单词/字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19987389/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 17:28:16  来源:igfitidea点击:

Find the longest word/string in an array

javascriptarrays

提问by lmission

I just started learning JavaScript. I am trying to write a JavaScript to find and print the longest word in an Array. I came up with the code below:

我刚开始学习 JavaScript。我正在尝试编写一个 JavaScript 来查找和打印数组中最长的单词。我想出了下面的代码:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"]
var longest = 0;
for (var i = 0; i < longWords.length; i++) {
if (longest < longWords[i].length) {
    longest = longWords[i];
  }
}

console.log(longest)

The problem is it always end up printing the first element in the array. which means longest = longWords[0]. Now when I change the line longest = longWords[i]to longest = longWords[i].lengthI get the count of the longest character. Please tell me why it didn't work and how I can do this using the forloop.

问题是它总是最终打印数组中的第一个元素。这意味着longest = longWords[0]. 现在,当我将行更改longest = longWords[i]longest = longWords[i].length我得到最长字符的计数。请告诉我为什么它不起作用以及我如何使用for循环来做到这一点。

回答by jbabey

if (longest < longWords[i].length) {

Should probably be

应该是

if (longest.length < longWords[i].length) {

回答by Sampson

You can custom sort based on string length, and grab the first item:

您可以根据字符串长度自定义排序,并抓取第一项:

longWords.sort(function(a, b) { 
    return b.length - a.length; 
});

This turns your array into the following:

这会将您的数组变成以下内容:

["Czechoslovakia", "Aubumayang", "Penelope", "Slovenia", "Johny"]

At that point, you can grab the first item. Note however that there may be other strings immediately following the first that are of the same length.

此时,您可以抓取第一个项目。但是请注意,在第一个字符串之后可能还有其他长度相同的字符串。

As for your above code, longestis declared as a number, but later set to a string. The number we're interested in comes from the length of the string. Our condition should be:

至于你上面的代码,longest被声明为一个数字,但后来设置为一个字符串。我们感兴趣的数字来自字符串的长度。我们的条件应该是:

// No sense in looking this up twice
current = longWord[i];

if ( longest.length < current.length ) {
    longest = current;
}

回答by David says reinstate Monica

Rather than finding the longest word, I'd suggest sorting the array by descending length of its elements, using Array.prototype.sort():

与其找到最长的单词,我建议使用Array.prototype.sort()以下方法按元素的降序长度对数组进行排序:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"],
    sorted = longWords.sort(function (a, b) {
    return a.length < b.length;
});

console.log(sorted);
// ["Czechoslovakia", "Aubumayang", "Penelope", "Slovenia", "Johny"]

JS Fiddle demo.

JS小提琴演示

References:

参考:

回答by Juan Gaitán

I could be easy to print the longest word if you sortthe array and print the first element arr.[0]

如果您sort使用数组并打印第一个元素,我可以轻松打印最长的单词arr.[0]



var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"];
var sorted = longWords.sort((a, b) => b.length - a.length );

console.log(sorted[0]);
// "Czechoslovakia"


There's another nice way to get the longest element, and that is with the reducemethod:

还有另一种获得最长元素的好方法,那就是使用以下reduce方法:

var longWords = ["Penelope", "Johny", "Aubumayang", "Czechoslovakia", "Slovenia"];

var longest = longWords.reduce(
  (a, b) => a.length >= b.length ? a : b
);

console.log(longest);

回答by adeneo

var longest = 0;

for (var i = 0; i < longWords.length; i++) {
    if ( longWords[i].length > longest.length ) {
        longest = longWords[i];
    }
}