简体   繁体   中英

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)

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM