I am looking into how to filter an array of data in Angular2.
I looked into using a custom pipe, but I feel this is not what I am looking for, as it seems more geared towards simple presentation transformations rather then filtering large sets of data.
The array is set out as follows:
getLogs(): Array<Logs> {
return [
{ id: '1', plate: 'plate1', time: 20 },
{ id: '1', plate: 'plate2', time: 30 },
{ id: '1', plate: 'plate3', time: 30 },
{ id: '2', plate: 'plate4', time: 30 },
{ id: '2', plate: 'plate5', time: 30 },
{ id: '2', plate: 'plate6', time: 30 }
];
}
I want to filter this by id. So when I enter "1" into a search bar, it updates to display the corresponding values.
If there is a method on how to do this, I would love to know!
To filter an array of objects in React:Call the filter() method on the array. On each iteration, check if a certain condition is met. The Array. filter methods returns an array with all elements that satisfy the condition.
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.
In Typescript, Filter() is a built-in array method which is defined as a method for creating a new array or set of elements that contains a subset of the given array elements by returning the array of all the values of the elements in the newly created sub-array over the given array.
You can use the filter () method to filter out elements in an array by their property values. This article will discuss the complete detail of filtering in Angular 2. Angular 2 is an MVW Framework built on TypeScript and has a model-view-whatever architecture. It’s a complete rewrite of Angular 1 and takes advantage of the latest web technologies.
Our app is written in Angular and display items via an item observable. The problem is we need to filter the observable based on a certain query and here’s the solution for it. Yes, it’s really that simple. Pipe the item array in the itemObservable, map each item in the array and check whether or not it matches the condition.
Angular 2 is an MVW Framework built on TypeScript and has a model-view-whatever architecture. It’s a complete rewrite of Angular 1 and takes advantage of the latest web technologies. In Angular 2, arrays are represented by the type Array. For example, an array of strings could be declared as follows.
The filter filter selects a subset of an array. The filter filter can only be used on arrays, and it returns an array containing only the matching items. By setting the ng-model directive on an input field, we can use the value of the input field as an expression in a filter.
There is no way to do that using a default pipe. Here is the list of supported pipes by default: https://github.com/angular/angular/blob/master/modules/angular2/src/common/pipes/common_pipes.ts.
That said you can easily add a pipe for such use case:
import {Injectable, Pipe} from 'angular2/core';
@Pipe({
name: 'myfilter'
})
@Injectable()
export class MyFilterPipe implements PipeTransform {
transform(items: any[], args: any[]): any {
return items.filter(item => item.id.indexOf(args[0]) !== -1);
}
}
And use it:
import { MyFilterPipe } from './filter-pipe';
(...)
@Component({
selector: 'my-component',
pipes: [ MyFilterPipe ],
template: `
<ul>
<li *ngFor="#element of (elements | myfilter:'123')">(...)</li>
</ul>
`
})
Hope it helps you, Thierry
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With