TypeError: Cannot read property 'search' of undefine
TypeError: Cannot read property 'search' of undefined
嗨,我正在尝试使用数组进行简单的搜索,但我得到这个错误:
TypeError: Cannot read property 'search' of undefined
这是我的代码:
var debug = [];
var search = function (data, wordSearch) {
"use strict";
var
wordList = [], //set a word lists to send a result.
i = 0,
word,
cutedWord;
debug.push(data);
while (i <= data.length) {
word = data[i];
if (word.search(wordSearch) !== -1) {
wordList.push(data[i]);
}
i++;
}
return (wordList);
};
从<=
中取出=
while (i < data.length) {
如果数组长度为0,即使它没有内容,它也将至少进行一次迭代,因为i<=0
和由于数组没有内容data[0]
将是未定义的,这使得word
未定义。
或者如果有内容,它将越过数组边界,结果相同
相关文章:
- Lucene Search in Alfresco
- HighChart : Usage of setData
- JSON and jQuery Search
- Difference between methods of defining JavaScript 'class
- 为什么我在下面的..of循环中得到意外令牌
- Appending the innerhtml of <th>
- Javascript Regex Search
- Animation cuts off multiple lines of text in <p>
- javascript: functionname() instread of functionname()
- Search a Sencha TreePicker
- jQuery使用.of()删除事件处理程序时出现的问题
- Three.js issue creating meshes outside of loader's load(
- jquery in the middle of html
- JavaScript equivalent of C#'s DynamicObject?
- nodejs console.log of object
- 要筛选的数组的管道值的含义:$select.search in angular
- value of jquery sortable
- Javascript imageData returns array of 0's
- 对于grunt构建中的(let.of)错误
- TypeError: Cannot read property 'search' of undefine