1

I am trying to implement a search feature on an existing array but I am getting an error stating that 'filter is not a function'. Maybe I am not passing in the correct parameters?

 searchValue = '';  
 stringResult = [];
 fields = ['Name'];    

 this.stringResult = this.sectionInvLineItems;
 this.arrayFields = this.fields;
 this.searchVal = this.searchValue;

 console.log( 'stringResult 2 ' + this.stringResult );  // [object Object]
 console.log( 'arrayFields 2 ' + this.arrayFields );  //returns Name 
 console.log( 'searchVal 2 ' + this.searchVal ); //returns what is being typed 

 const filterData = (stringResult, arrayFields, searchVal) => {
   return stringResult.filter( item => !item[arrayFields].toLowerCase().search(searchVal.toLowerCase()));
 };

 if(  this.searchValue.length > 2 ) {
   const remaining = filterData( this.stringResult, this.arrayFields, this.searchValue );

   this.stringResult = [ ...remaining ];
 }
 else {
   this.displayedItems = [ ...this.stringResult ];
 }

1 Answer 1

3

This error occurs because the filter method is being called on a string instead of an array (line 18):

if(  this.searchValue.length > 2 ) {
  // When filterData is called this.stringResult is equal to a string instead of an array 
  const remaining = filterData( this.stringResult, this.arrayFields, this.searchValue );

  this.stringResult = [ ...remaining ];
}

To resolve this error you'll need to update line 5 to set this.stringResult equal to an array.

7
  • Thank you for the reply Tyler. I used JSON.stringify so that I could see the data. ex: items":[{"detail":{"Id":"asasasasasasasas","Name":"CI-000003","Section__c":"General Section". So are you saying do not use JSON.stringify? stringResult is an array but I need to populate the array? Or do i keep JSON.stringify and use Object.fromEntries to create the array? Thnak you.
    – Phuc
    Commented Mar 7, 2022 at 3:02
  • If you are only using JSON.stringify to view the contents of the variable, then I would recommend moving JSON.stringify from line 5 to the console log statement: this.stringResult = this.sectionInvLineItems; console.log( 'stringResult 2 ' + JSON.stringify(this.stringResult)); //returns array data This will ensure that the variable is an array instead of a string when you call the .filter method. Commented Mar 7, 2022 at 12:35
  • Tyler, I made the updates as you suggested so now in the console this.stringResult will render [object Object] . When I step through in the inspector console I can the array data under t : OriginalTarget: Items. But I am still getting a script error
    – Phuc
    Commented Mar 7, 2022 at 13:39
  • Tyler, do you know if the array that holds the fields I am searching against need to be fields = ['Name'] or fields = ['Name:'];
    – Phuc
    Commented Mar 7, 2022 at 15:01
  • Hey Tyler. The issue is that the array keep reading undefined. Error: ' Cannot read properties of undefined' . Like I said before I can see the array data under Proxy/Handler which I thought was normal. Please advise if this could be the issue. Thanks
    – Phuc
    Commented Mar 7, 2022 at 22:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .