Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering an array in angular2

Tags:

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!

like image 782
Witted Avatar asked Dec 22 '15 13:12

Witted


People also ask

How do you filter an array in react JS?

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.

What is filter method in angular?

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.

How does filter work in TypeScript?

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.

How to filter out elements in an array in Angular 2?

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.

How to filter an observable based on a query in angular?

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.

What is an array type 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. In Angular 2, arrays are represented by the type Array. For example, an array of strings could be declared as follows.

What is a filter filter in JavaScript?

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.


1 Answers

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

like image 91
Thierry Templier Avatar answered Sep 20 '22 19:09

Thierry Templier