1

I have an issue with a custom pipe I am creating. My Persos is an array of Perso objects and it seems like I can't apply .filer() so I tried a simple for loop ( which works fine in a *ngFor on such Persos var ) with no success either. I think I am missing something very basic here about handling objects in Typescript.

Here is the pipe code with some tests and comments:

import { Pipe, PipeTransform } from '@angular/core';

import { Perso } from './perso';

@Pipe({ name: 'startsWithPersoPipe' })
export class StartsWithPersoPipe implements PipeTransform {
  transform(Persos: Perso[]){

    // this for makes it all bug, when I comment it my pipe works fine
    for( let perso of Persos){
    }

    // this for does not make it crash but does not behave at all like I want
    for( let perso in Persos){
      console.log(perso); // just prints 0, 1, 2, 3 etc up to 109 ( i have 110 elements in the Perso[] var I use for test)
      console.debug(perso); // same behavior as console.log(perso)
      console.log(perso.nom); // undefined
    }
    console.debug(Persos); // Array [ Object, Object, Object, Object, Object, Object, Object, Object, Object, Object, 100 more… ]
                          // when I click on on of the Object I get one of my objects with correct values in nom, description and type : it's all fine !
    return Persos;
  }
}

Here is perso.ts

export class Perso {
  nom: string;
  type: string;
  description: string;
}

Any help, hint or link to useful resource that help me solve this is much appreciated.

2
  • What does "can't use .filter()" mean exactly? What is the desired behavior? Commented Aug 16, 2016 at 9:11
  • it's like the for of, makes it crash. Ultimately I want to filter out all Perso objects which "nom" starts with "Super" but that part isn't what made it bug so i removed all unnecessary things to show only the faulty code here. Commented Aug 16, 2016 at 9:16

1 Answer 1

1

This should do:

@Pipe({ name: 'startsWithPersoPipe' })
export class StartsWithPersoPipe implements PipeTransform {
  transform(Persos: Perso[]){
    if(Persos == null) {
      return null;
    }

    return persos.filter(p => p.nom && p.nom.startsWith('super'));
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

if(Persos == null) { return null; } is what made it all work. Thanks a lot ! This is quite puzzling for me as in angular.io/docs/ts/latest/guide/pipes.html#flying-heroes-pipe they don't do that ( cf app/flying-heroes.pipe.ts ). When should I ? Why don't they need it and I do ?
I guess they just don't pass in null ;-). You should have got a meaningful error message when .filter(...) is called on null.
as you can see in console.debug Persos it is not a null value. Moreover I just added the if(Persos == null) { return null; } to my code and the rest worked, meaning Persos wasn't null but for some reason there is a need for verification. Maybe my pipe function is casted before angular gives it its value to Persos, then Persos get initialized and pipe applied again.
I guess it is first called with null and later when the value is set again with a non-null value. Just checking for null doesn't do anything.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.