Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular 4 - .filter is not a function

I have this code:

 transform(searchData: Array<vw_WebSiteCourseSearch>, searchResultContentType: string) {
        if (searchData == undefined) {
            return;
        }

        return searchData.filter((item) => item.ContentType == searchResultContentType);
    }

My console prints:

ERROR TypeError: searchData.filter is not a function
at FilterCountPipe.webpackJsonp.../../../../../src/app/Common/pipes/filterCount.pipe.ts.FilterCountPipe.transform (filterCount.pipe.ts:19)

I tried to add import from rxjs/add/operator/filter, but it didn't solved it. Any ideas on how to fix it?

Thanks

like image 626
Aa Yy Avatar asked Mar 21 '18 16:03

Aa Yy


People also ask

Can filter () execute the function for array elements without values?

The filter() method does not execute the function for empty elements.

Is not a function JavaScript object?

A TypeError: "x" is not a function occurs when a function is called on an object that does not contain the called function. When calling a built-in function that expects a callback function argument, which does not exist. When the called function is within a scope that is not accessible.

Is not a function TypeError is not a function?

This is a standard JavaScript error when trying to call a function before it is defined. This error occurs if you try to execute a function that is not initialized or is not initialized correctly. This means that the expression did not return a function object.

How filter works in AngularJS?

The “filter” Filter in AngularJS is used to filter the array and object elements and return the filtered items. In other words, this filter selects a subset (a smaller array containing elements that meet the filter criteria) of an array from the original array.


1 Answers

Should searchData be null (or undefined), you should get an error like

TypeError: cannot read property filter of null  // or undefined

Like @OscarPaz thought, filter is not a function is thrown because the received searchData is not an array (yet still defined and not null).

like image 68
Matei Radu Avatar answered Oct 08 '22 17:10

Matei Radu