Tyler L Tyler L - 3 years ago 327
Javascript Question

indexOf is not a function on an array in jQuery grep

Here's my function

function search(){
var searchValue = $('#search-bar').val();

console.log(Array.isArray(initData[0].body)); //true

returnArr = $.grep(initData[0].body, function( element, idx ) {
return element.indexOf(searchValue) != -1; // error: indexOf is not a function
});

}


I'm getting
indexOf
is not a function.

I know initData[0].body is an array (see code)

Answer Source

You are calling indexOf on the element variable which is an item inside your initData[0].body array. Make sure element is an array type if you are calling indexOf on it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download