javascript - Javascript - 如何找到字符串中最长的单词,并返回那些(排除重复项)以及最大长度?

如何在字符串中查找最长的单词,


function longestWord(sentence) {


 sentence = sentence.split(' ');



 let theWord = sentence[0];


 var longest = 0;


 for (let i = 0; i < sentence.length; i++) {


 if (sentence[i] !="") {


 if (sentence[i].length > theWord.length) {


 longest = sentence[i].length;


 theWord = sentence[i];


 }


 }


 }


 return {


 length: longest,


 actuallWord: theWord


 }


}


console.log(longestWord("The quick brown as bbbbbb fox jumped over the bbbbbb lazy dog"));

时间:


function longestWord(sentence) {


 // First we divide the sentence into words


 var words = sentence.split(' ');



 // We then get the length by getting the maximun value of the length of each word


 var length = Math.max(...words.map(a=>a.length));


 return {


 length: length,


 // Finally we filter our words array returning only those of with the same length we previously calculated


 words: words.filter(i => i.length == length)


 }


}


console.log(longestWord("The quick brown as bbbbbb fox jumped over the lazy dog"));

你可以采用单循环方法,


function longestWords(words) {


 return words


 .split(/s+/)


 .reduce((accu, word) => {


 if (!accu[0] || accu[0].length < word.length) return [word];


 if (accu[0].length === word.length) accu.push(word);


 return accu;


 }, []);


}



console.log(longestWords("The quick brown as bbbbbb fox jumped over the lazy dog"));

可以通过数组Array.prototype.reduce()的一次传递来使用(无需额外的循环即可计算最大长度)。


const src = 'The quick brown as bbbbbb fox jumped over the jumped lazy dog',


 result = src.split(' ')


 .reduce((res, word, idx) => (


 !idx || word.length > res.words[0].length ? 


 res = {length: word.length, words:[word]} : 


 word.length == res.words[0].length &&


 !res.words.includes(word) ? 


 res.words.push(word) : 


 true, 


 res), {})



console.log(result)
.as-console-wrapper {min-height:100%}

可以通过减少sentence数组来实现。

这种方法的优点是它只循环一次数组:


function longestWords(sentence) {


 return sentence.split(' ').reduce((output, word) => {


 if (word.length > output.length) {


 output.words = [word]


 output.length = word.length


 } else if (word.length === output.length) {


 output.words.push(word)


 }


 return output


 }, { length: 0, words: [] })


}


console.log(longestWords("The quick brown as bbbbbb fox jumped over the lazy dog"));

或者,如果要筛选出重复的单词,可以改为返回Set


function longestWords(sentence) {


 return sentence.split(' ').reduce((output, word) => {


 if (word.length > output.length) {


 output.words.clear()


 output.length = word.length


 } 


 if (word.length >= output.length) {


 output.words.add(word)


 }


 return output


 }, { length: 0, words: new Set })


}


const words = longestWords("The quick brown as bbbbbb fox jumped over the jumped lazy dog")


console.log(words.length);


console.log(Array.from(words.words)) //Just to make StackSnippets console show the Set's entries


用对象启动。


function longestWord(words) {


 return words


 .split(/s+/)


 .reduce((acc, word) => {


 	if(word.length > acc.length) {


 		acc.length = word.length;


 		acc.words = [word];


 	}


 	else if (word.length === acc.length) {


 		acc.words.push(word);


 	}


 	return acc;


 }, {length:0, words:[]});


}



console.log(longestWord("The quick brown as bbbbbb fox jumped over the lazy dog"));

...