efficient angular2 filter that loops over list and a sub-list

145 Views Asked by At

I have an angular2 filter that I am not to pleased with.

The filter loops through an array of companies and every company has an employee array with multiple employees.

  1. When the input the filter receives consists of less than 3 characters it only checks the values in the company object and the first employee object.
  2. When the input exceeds 3 characters the filter also checks the values of the other employee objects in the company.

This filter works fine but to me it seems poorly optimised and just plain ugly.
Is there a more efficient way to write this filter?


The filter code:

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

@Pipe({
  name: 'filter',
  pure: false
})
export class SpFilterPipe implements PipeTransform {

  transform(value: any, args?: any): any {
    if (value.length === 0) {
      return value;
    }
    let resultArray = [];
    for (let item of value) {
      if (
      ~item.name.toLowerCase().indexOf(args) ||
      ~item.employees[0].displayname.toLowerCase().indexOf(args) ||
      ~item.employees[0].fullname.toLowerCase().indexOf(args)
      ) {
        resultArray.push(item);
      }
      if (args.length > 2) {
        for (let employee of item.employees) {
          if(
              ~employee.displayname.toLowerCase().indexOf(args) ||
              ~employee.fullname.toLowerCase().indexOf(args)
          ) {
            if (item.employees.indexOf(employee)){
              resultArray.push(item);
            }
          }
        }
      }
    }

    return resultArray;
  }
}


And the JSON object i'm looping over:

{
  "companies" : [ {
    "employees" : [ {
      "displayname" : "F.H.Willemsen",
      "fullname" : "Fred Harold Willemsen",
      "number" : "001"
    }, {
      "displayname" : "D.Muys",
      "fullname" : "Dennis Muys",
      "number" : "002"
    } ],
    "floor" : 21,
    "name" : "nothing",
    "room" : "21.21 - 31.31"
  }, {
    "employees" : [ {
      "displayname" : "A.Testingperson",
      "fullname" : "Adriaan Testingperson",
      "number" : "003"
    }, {
      "displayname" : "B.Muys",
      "fullname" : "Bassie Muys",
      "number" : "004"
    } ],
    "floor" : 20,
    "name" : "TestingCompany",
    "room" : "20.20 - 30.30"
  }, {
    "employees" : [ {
      "displayname" : "D.Kort",
      "fullname" : "Dirk Kort",
      "number" : "005"
    }, {
      "displayname" : "K.Lang",
      "fullname" : "Karel Lang",
      "number" : "006"
    } ],
    "floor" : 22,
    "name" : "Bla",
    "room" : "22.22 - 32.32"
  }, {
    "employees" : [ {
      "displayname" : "A.Panamera",
      "fullname" : "Abdullah Panamera",
      "number" : "007"
    }, {
      "displayname" : "D.Nguyen",
      "fullname" : "Dong Nguyen",
      "number" : "008"
    } ],
    "floor" : 23,
    "name" : "Apple Dell",
    "room" : "23.23 - 33.33"
  }, {
    "employees" : [ {
      "displayname" : "B.Kadjar",
      "fullname" : "Barry Kadjar",
      "number" : "009"
    }, {
      "displayname" : "G.Option",
      "fullname" : "Gerard Option",
      "number" : "010"
    } ],
    "floor" : 24,
    "name" : "Magic Trackpad",
    "room" : "24.24 - 34.34"
  } ]
}
0

There are 0 best solutions below