angular2 pipe for multiple arguments

Han Che

I have a array of thread objects, each thread object with the properties

unit:number
task:number
subtask:number

I want to create a pipe to filter after these threads, so far I have a working pipe like below. I'm not really satisfied with it yet and wanted to ask you guys if there is a more elegant solution?

HTML:

<div class="thread-item" *ngFor="#thread of threadlist | threadPipe:unitPipe:taskPipe:subtaskPipe"></div>

Pipe.ts

export class ThreadPipe implements PipeTransform{

  threadlistCopy:Thread[]=[];

  transform(array:Thread[], [unit,task,subtask]):any{

    //See all Threads
    if(unit == 0 && task == 0 && subtask == 0){
      return array
    }
    //See selected Units only
    if(unit != 0 && task == 0 && subtask == 0){
      this.threadlistCopy=[];
      for (var i = 0; i<array.length;i++){
        if(array[i].unit == unit){
          this.threadlistCopy.push(array[i])
        }
      }
      return this.threadlistCopy
    }
    //See selected Units and Tasks
    if (unit != 0 && task != 0 && subtask == 0){
      this.threadlistCopy=[];
      for (var i = 0; i<array.length;i++){
        if(array[i].unit == unit && array[i].task == task){
          this.threadlistCopy.push(array[i])
        }
      }
      return this.threadlistCopy
    }
    // See selected units, tasks, subtask
    if (unit != 0 && task != 0 && subtask != 0){
      this.threadlistCopy=[];
      for (var i = 0; i<array.length;i++){
        if(array[i].unit == unit && array[i].task == task && array[i].subtask == subtask){
          this.threadlistCopy.push(array[i])
        }
      }
      return this.threadlistCopy
    }
  }
}
Yaron Schwimmer

You are implementing your pipe the right way, but you are basically re-inventing the Array.prototype.filter mechanism in your code. A simpler way will be:

export class ThreadPipe implements PipeTransform{

  transform(array:Thread[], [unit,task,subtask]):any{

    //See all Threads
    if(unit == 0 && task == 0 && subtask == 0){
      return array
    }
    //See selected Units only
    if(unit != 0 && task == 0 && subtask == 0){
      return array.filter(thread => {
        return thread.unit === unit;
      });
    }
    //See selected Units and Tasks
    if (unit != 0 && task != 0 && subtask == 0){
      return array.filter(thread => {
        return thread.unit === unit && thread.task === task;
      });
    }
    // See selected units, tasks, subtask
    if (unit != 0 && task != 0 && subtask != 0){
      return array.filter(thread => {
        return thread.unit === unit && thread.task === task && thread.subtask === subtask;
      });
    }
  }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Watch + pipe + multiple arguments

From Java

How do I call an Angular 2 pipe with multiple arguments?

From Dev

angular2 - pipe - filter multiple json values

From Dev

pipe as arguments

From Dev

angular2 - pipe error

From Dev

How to pipe Enum output to another Enum function that takes multiple arguments?

From Dev

Pipe multiline powershell results into multiple arguments in one line

From Dev

Angular2: pass in a property name to a pipe

From Dev

Angular2: No Pipe decorator found on Component

From Dev

Testing Angular2 / TypeScript pipe with Jasmine

From Dev

Angular2 split string (pipe?)

From Dev

Error handling with Angular2 async pipe

From Dev

Angular2 - assign pipe from a variable

From Dev

Assign the output of a pipe in angular2 to a variable

From Dev

Angular2 - assign pipe from a variable

From Dev

Angular2: No Pipe decorator found on Component

From Dev

Angular2 async pipe with observable

From Dev

Angular2 pipe not working for input

From Dev

Angular2 filter pipe on input

From Dev

Angular2: Resolve a string to a pipe

From Dev

Angular2 Pipe to convert currency

From Dev

Angular2 pipe regex url detection

From Dev

Angular2 pipe dynamic value not taking

From Dev

Pass arguments with pipe in bash

From Java

The pipe ' ' could not be found angular2 custom pipe

From Dev

Angular2 use basic pipe in custom pipe

From Java

Pass multiple values to pipe in Angular 6

From Java

Angular ng-if="" with multiple arguments

From Dev

How to pass multiple arguments with pipe symbol into a bash-script's case statement