If you've ever used JavaScript's split method, there's a good chance that you've encountered the following error: TypeError: Cannot read property 'split' of undefined.
如果你曾经使用过 JavaScript 的 split 方法,很有可能你遇到了下面的错误: TypeError: Can not read property‘ split’of undefined。
There are a few reasons why you would receive this error. Most likely it's just a basic misunderstanding of how split works and how to iterate through arrays.
functionfindLongestWord(str){for(let i =0; i < str.length; i++){const array = str.split(" ");
array[i].split("");}}
findLongestWord(“The quick brown fox jumped over the lazy dog”);
it will throw the TypeError: Cannot read property ‘split’ of undefined error.
它将抛出 TypeError: Can not read property‘ split’of undefined error。
The split method
分裂法
When split is called on a string, it splits the string into substrings based on the separator passed in as an argument. If an empty string is passed as an argument, split treats each character as a substring. It then returns an array containing the substrings:
Knowing what the split method returns and how many substrings you can expect is the key to solving this challenge.
知道 split 方法返回什么以及您可以期望有多少子字符串是解决这个问题的关键。
Let’s take another look at the code above and see why it’s not working as expected:
让我们再看一下上面的代码,看看为什么它不能正常工作:
functionfindLongestWord(str){ for(let i =0; i < str.length; i++){ const array = str.split(" “);
array[i].split(”"); } }
findLongestWord(“The quick brown fox jumped over the lazy dog”);
Splitting str into an array like this (const array = str.split(" “);) works as expected and returns [ ‘The’, ‘quick’, ‘brown’, ‘fox’, ‘jumped’, ‘over’, ‘the’, ‘lazy’, ‘dog’ ].
But take a closer look at the for loop. Rather than using the length of array as a condition to iterate i, str.length is used instead.
但是仔细看看 for 循环。不使用数组的长度作为迭代 i 的条件,而是使用 str.length。
str is “The quick brown fox jumped over the lazy dog”, and if you log str.length to the console, you’ll get 44.
Str 是“ The quick brown fox jumped over The lazy dog”,如果你将 str.length 记录到控制台,你会得到44。
The last statement in the body of the for loop is what’s causing the error: array[i].split(”");. The length of array is 9, so i would quickly go way over the maximum length of array:
For 循环体中的最后一条语句是导致错误的原因: array [ i ].split(””) ;。数组的长度是9,所以我会快速地超过数组的最大长度:
findLongestWord(“The quick brown fox jumped over the lazy dog”);
Calling array[i].split(""); to split each string into substrings of characters is a valid approach, but it will throw TypeError: Cannot read property ‘split’ of undefined when it’s passed undefined.
How to solve Find the Longest Word in a String with split
如何解决字符串中最长单词的分割问题
Let’s quickly go over some pseudo code for how to solve this problem:
让我们快速浏览一些解决这个问题的伪代码:
Split 分裂str into an array of individual words 变成一系列单词
Create a variable to track the greatest word length 创建一个变量来跟踪最大的单词长度
Iterate through the array of words and compare the length of each word to the variable mentioned above 循环遍历单词数组,并将每个单词的长度与上面提到的变量进行比较
If the length of the current word is greater than the one stored in the variable, replace that value with the current word length 如果当前单词的长度大于存储在变量中的单词的长度,则用当前单词长度替换该值
Once the length of every word is compared with the maximum word length variable, return that number from the function 将每个单词的长度与最大单词长度变量进行比较后,从函数中返回该数字
First, split str into an array of individual words:
functionfindLongestWordLength(str){ const array = str.split(” "); let maxWordLength =0;
for(let i =0; i < array.length; i++){
} }
Iterate through the array of words and check the length of each word. Remember that strings also have a length method you can call to easily get the length of a string:
functionfindLongestWordLength(str){ const array = str.split(" "); let maxLength =0;
for(let i =0; i < array.length; i++){ array[i].length; } }
Use an if statement check if the length of the current word (array[i].length) is greater than maxLength. If so, replace the value of maxLength with array[i].length:
使用 if 语句检查当前单词的长度(数组[ i ])。长度)大于 maxLength。如果是,将 maxLength 的值替换为 array [ i ]。长度:
functionfindLongestWordLength(str){ const array = str.split(" "); let maxLength =0;
for(let i =0; i < array.length; i++){ if(array[i].length > maxLength){ maxLength = array[i].length; } } }
Finally, return maxLength at the end of the function, after the for loop:
最后,在函数的末尾,在 for 循环之后返回 maxLength:
functionfindLongestWordLength(str){ const array = str.split(" "); let maxLength =0;
for(let i =0; i < array.length; i++){ if(array[i].length > maxLength){ maxLength = array[i].length; } }