最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: undefined is not an object - when evaluating an array with forEach - Stack Overflow

programmeradmin6浏览0评论

I am making a search bar, and I'm having a weird error that has not been solved by numerous other posts of the same error. I have an array, and I am using forEach to log to the console depending on the index of the array. However, I am getting this error:

TypeError: undefined is not an object (evaluating 'sites[index].indexOf')

My code is as follows:

var sites = ["Website 1", "Website 2", "Youtube Test Page", "Go to google", "testing for foreward slashes", "MORE!!! :)", "beachballs", "test2", "Good Luck"];

function search() {
    var input = $("#searchBar")[0].value;
    sites.forEach(function(index) {
        console.log(input);
        if (sites[index].indexOf(input) != -1) {
            console.log("yay");
        }
    })
}

And here is my search bar:

<input type="text" placeholder="Search the web" id="searchBar" onkeyup="search()"/>

I have done a lot of research to no avail, so I'd appreciate some help.

I am making a search bar, and I'm having a weird error that has not been solved by numerous other posts of the same error. I have an array, and I am using forEach to log to the console depending on the index of the array. However, I am getting this error:

TypeError: undefined is not an object (evaluating 'sites[index].indexOf')

My code is as follows:

var sites = ["Website 1", "Website 2", "Youtube Test Page", "Go to google", "testing for foreward slashes", "MORE!!! :)", "beachballs", "test2", "Good Luck"];

function search() {
    var input = $("#searchBar")[0].value;
    sites.forEach(function(index) {
        console.log(input);
        if (sites[index].indexOf(input) != -1) {
            console.log("yay");
        }
    })
}

And here is my search bar:

<input type="text" placeholder="Search the web" id="searchBar" onkeyup="search()"/>

I have done a lot of research to no avail, so I'd appreciate some help.

Share Improve this question edited Sep 16, 2018 at 21:11 Sebastian Simon 19.6k8 gold badges61 silver badges84 bronze badges asked Sep 11, 2018 at 5:08 Jack BashfordJack Bashford 44.2k11 gold badges55 silver badges82 bronze badges 0
Add a ment  | 

3 Answers 3

Reset to default 6

sites is an array, not a jQuery collection, so when you call forEach on it, the first argument to the callback is the item being iterated over, not the index. Change to:

sites.forEach(function(site) {
    console.log(site);
    if (site.indexOf(input) != -1) {
        console.log("yay");
    }
})
var sites = ["Website 1", "Website 2", "Youtube Test Page", "Go to google", "testing for foreward slashes", "MORE!!! :)", "beachballs", "test2", "Good Luck"];

function search() {
    var input = $("#searchBar")[0].value;
    sites.forEach(function(value,index) {
        console.log(input);
        if (sites[index].indexOf(input) != -1) {
            console.log("yay");
        }
    })
}

Array.forEach callback has 3 parameters.

callback is invoked with three arguments:

the element value

the element index

the array being traversed

arr.forEach(function callback(currentValue[, index[, array]]) {
    //your iterator
});

as for your case you only need the first parameter currentValue.

Read more Array.prototype.forEach() - Javascript | MDN

发布评论

评论列表(0)

  1. 暂无评论