0

I want to make filter about an array of object

To make this filter I got this pipe :

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

@Pipe({
  name: 'filter'
})
export class FilterPipe implements PipeTransform {
  transform(items: any[], searchText: string[]): any[] {
    if (!items) { return []; }
    if (!searchText || searchText.length <= 0) { return items; }

    return items.filter(item => {
      return Object.keys(item).some(key => {
        return String(item[key]).toLowerCase().includes(searchText[0]);
      });
    });
  }
}

My array is :

[
   {
      "drinkPreference":"Apple",
      "email":"[email protected]",
      "firstName":"fgsfdg",
      "id":"_ihaj8q0uh",
      "lastName":"Test"
   },
   {
      "drinkPreference":"NoDrink",
      "email":"[email protected]",
      "firstName":"Marc",
      "id":"_rhahpcz3h",
      "lastName":"Lander"
   },
   {
      "drinkPreference":"Banana",
      "email":"[email protected]",
      "firstName":"fdgsdg",
      "id":"_5l63di4x5",
      "lastName":"fdgfsd"
   },
   {
      "drinkPreference":"NoDrink",
      "email":"[email protected]",
      "firstName":"qfdqsfdsq",
      "hobbies":[
         "qsfqd"
      ],
      "id":"_byvwezzs4",
      "lastName":"look"
   }
]

And for example my filter array:

   arrayfilter = ["Lander","Test"]

It have to return me :

[
   {
      "drinkPreference":"Apple",
      "email":"[email protected]",
      "firstName":"fgsfdg",
      "id":"_ihaj8q0uh",
      "lastName":"Test"
   },
   {
      "drinkPreference":"NoDrink",
      "email":"[email protected]",
      "firstName":"Marc",
      "id":"_rhahpcz3h",
      "lastName":"Lander"
   }
]

But it return me only elements who match with my first item of arrayfilter so "Lander" items.

How can I modify my pip function to got all results ?

Thanks !

1
  • "But it return me only elements who match with my first item of arrayfilter" - Because you're telling it to do so: .includes(searchText[0]) Commented Mar 3, 2020 at 14:34

1 Answer 1

2

As you get only the first element from the array:

searchText[0]

So you are filtering just by the first item of an array.

You can use Object.values and do not forget to use toLowerCase() for both string:

searchText = searchText.map(s => s.toLowerCase());
return items.filter(f => 
    Object.values(f).some(s => 
        searchText.includes(typeof s == 'string' ? s.toLowerCase() : s)));

An example:

let myArray = [
    {
       "drinkPreference":"Apple",
       "email":"[email protected]",
       "firstName":"fgsfdg",
       "id":"_ihaj8q0uh",
       "lastName":"Test"
    },
    {
       "drinkPreference":"NoDrink",
       "email":"[email protected]",
       "firstName":"Marc",
       "id":"_rhahpcz3h",
       "lastName":"Lander"
    },
    {
       "drinkPreference":"Banana",
       "email":"[email protected]",
       "firstName":"fdgsdg",
       "id":"_5l63di4x5",
       "lastName":"fdgfsd"
    },
    {
       "drinkPreference":"NoDrink",
       "email":"[email protected]",
       "firstName":"qfdqsfdsq",
       "hobbies":[
          "qsfqd"
       ],
       "id":"_byvwezzs4",
       "lastName":"look"
    }
];

let arrayfilter = ["Lander","Test"];
arrayfilter = arrayfilter.map(s => s.toLowerCase());
const result = myArray.filter(f => 
    Object.values(f).some(s => 
        arrayfilter.includes(typeof s == 'string' ? s.toLowerCase() : s)));
console.log(result);

Sign up to request clarification or add additional context in comments.

Comments

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.