Hi.

I have take a look at the code. A lot of the stuff in there seems to be 
essential but I did edit the code.
I hope it can be of some help:

interface Dossier {
    datas: any[];   // I image this is an Array of a different interface.
    name: string;
}


@Pipe({name: 'search2'})
export class TopBarPipe implements PipeTransform {
    transform(dossier: Dossier[], searchTerm: string): Dossier[] {
        const copy: Dossier[] = [];
        if(!dossier) { return copy; };
        for (var i = 0, l = dossier.length; i < l; i++) {
            if (~dossier[i].name.toLowerCase().indexOf(searchTerm.
toLowerCase())) {
                copy.push(dossier[i]);
                continue;
            }
            for (var j = 0, m = dossier[i].datas.length; j < m; j++) {
                if (~(dossier[i].datas[j].designationProduit.toLowerCase()).
indexOf(searchTerm.toLowerCase())) {
                    if (copy.length === 0) {
                    // New dossier
                        copy.push({
                            datas: [dossier[i].datas[j]],
                            name: dossier[i].name
                        });
                    }
                    else {
                        for (var k = 0, n = copy.length; k < n; k++) {
                            if (copy[k].name === dossier[i].datas[j].
idDossier) {
                                copy[k].datas.push(dossier[i].datas[j]);
                            }
                            else {
                    // New dossier
                                copy.push({
                                    datas: [dossier[i].datas[j]],
                                    name: dossier[i].name
                                });
                            }
                        }
                    }
                }
            }
        }
        return copy;
    }
}


On Wednesday, January 24, 2018 at 2:02:09 PM UTC, [email protected] wrote:
>
>
> Hi there,
>
> Here is my @Pipe for a specific Array, i think that it can be far far 
> faster in the way to write it so i would like to see how i can improve it : 
> I use it in a mat-optgroup in a mat-autocomplete to have folders(dossier) 
> sorted and subfolders (datas here) too.
>
> @Pipe({name: 'search2'})
> export class TopBarPipe implements PipeTransform {
>   transform(dossier, searchTerm: string): any {
>     if(!dossier) return [];
>     var copy = [];
>     for (var i = 0; i < dossier.length; i++) {
>       var foundDossier = 0;
>       if (~dossier[i].name.toLowerCase().indexOf(searchTerm.toLowerCase()) 
> && !foundDossier) {
>         foundDossier = 1;
>         copy.push(dossier[i]);
>       }
>       if (!foundDossier) {
>         for (var j = 0; j < dossier[i].datas.length; j++) {
>           if 
> (~(dossier[i].datas[j].designationProduit.toLowerCase()).indexOf(searchTerm.toLowerCase()))
>  
> {
>             if (copy.length === 0) {
>               var newDossier = {};
>               newDossier.datas = [];
>               newDossier.name = dossier[i].name;
>               newDossier.datas.push(dossier[i].datas[j]);
>               copy.push(newDossier);
>             }
>             else {
>               for (var k = 0; k < copy.length; k++) {
>                 if (copy[k].name === dossier[i].datas[j].idDossier) {
>                   copy[k].datas.push(dossier[i].datas[j]);
>                 }
>                 else {
>                   var newDossier = {};
>                   newDossier.datas = [];
>                   newDossier.name = dossier[i].name;
>                   newDossier.datas.push(dossier[i].datas[j]);
>                   copy.push(newDossier);
>                 }
>               }
>             }
>           }
>         }
>       }
>     }
>     return copy;
>   }
> }
>
> The array is attachment.  
>
>
> Thank you all
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.

Reply via email to