Angular 2 Search Pipe filter objects with array property

Dan

I have Angular 2 Search Pipe that filters against an array of Project[]. It works for every property except for one containing an array of strings.

Here is a sample of data model

[{
    'Id': 2,
    'Title': 'Abc',        
    'Amount': '200',
    'Outcome': ['Outcome 2', 'Outcome 22', 'Outcome 222', 'Outcome 2222']
},
{
    'Id': 3,
    'Title': 'Abc',        
    'Amount': '300',
    'Outcome': ['Outcome 3', 'Outcome 333', 'Outcome 3333', 'Outcome 33333']
}]

Here is the SearchPipe -

not searching against Outcome array

export class SearchPipe implements PipeTransform {
transform(value, args?): Project[] {
    let searchText = new RegExp(args, 'ig');
    if (value) {
        return value.filter(project => {
            if (project) {
                return project.Title.search(searchText) !== -1
                    || project.Focus.search(searchText) !== -1
                    || project.Outcome.forEach(outcome => {
                        if (outcome.search(searchText) !== -1) {
                            return true;
                        }
                        else {
                            return false;
                        }
                    });
            }
        });
    }
}

}

Any Help would be much appreciated - Thank you!

Nige

Your foreach is incorrect. It doesnt return true or false. You can change your pipe to something like this and search if it contains something in the string and then return a boolean accordingly.

Like so:

@Pipe({name: 'Search'})
export class Search implements PipeTransform {
  transform(value, args?) {
      let searchText = 'test';

      if (value) {
          return value.filter(project => {
              if (project) {
                  return  !project.Outcome.every(outcome => {
                              return (!outcome.includes(searchText))
                          });

              }
          });
      }
  }
}

Also check the plunker I used to see it working ( https://plnkr.co/edit/ntyDUEwe0HXwjeupqDUr?p=preview )

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Angular2 Filter Array of Objects with pipe using RegExp?

From Dev

Filter object array with pipe Angular 2

From Dev

Array of Objects filter angular2

From Dev

Find and filter pipe for all content of Array as JSON format Angular 2

From Dev

Angular2/Typescript: How to get an array of objects through Pipe?

From Dev

Angular2 filtering array of objects based on an array of objects using pipe

From Dev

Swift filter array of objects by objects INT! property

From Dev

Angular2: Error while using a text filter pipe to filter an array

From Dev

How do I filter a javascript object when making a search pipe with angular 2?

From Dev

how to sort array of objects according to sub property in ng2-order-pipe

From Dev

How to filter by an array property in an array of objects

From Dev

Angular filter on two objects of an array

From Dev

Angular filter of array of objects by object

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular 2 pipe/filter with component variable

From Dev

Angular 2 pipe/filter with component variable

From Dev

angular 2 Pipe - filter by JSON key

From Dev

Angular 2 pipe to filter grouped arrays

From Dev

Angular2 filter pipe on input

From Dev

Angular2: pass in a property name to a pipe

From Dev

Filter an array of objects by a property containing a substring in AngularJS

From Dev

_.filter array of objects using a property with either value

From Dev

Filter array of objects where object property is date

From Dev

Filter by text search in some fields of array of objects

From Dev

angularjs custom filter for search in an Array of objects

From Dev

How to search by a property in array of objects in php?

From Dev

Can't use .filter() nor for on an array of objects in my pipe

From Dev

lodash filter array of objects by array Angular 5

From Dev

Angular2 filter/pipe using radio buttons to filter

Related Related

  1. 1

    Angular2 Filter Array of Objects with pipe using RegExp?

  2. 2

    Filter object array with pipe Angular 2

  3. 3

    Array of Objects filter angular2

  4. 4

    Find and filter pipe for all content of Array as JSON format Angular 2

  5. 5

    Angular2/Typescript: How to get an array of objects through Pipe?

  6. 6

    Angular2 filtering array of objects based on an array of objects using pipe

  7. 7

    Swift filter array of objects by objects INT! property

  8. 8

    Angular2: Error while using a text filter pipe to filter an array

  9. 9

    How do I filter a javascript object when making a search pipe with angular 2?

  10. 10

    how to sort array of objects according to sub property in ng2-order-pipe

  11. 11

    How to filter by an array property in an array of objects

  12. 12

    Angular filter on two objects of an array

  13. 13

    Angular filter of array of objects by object

  14. 14

    angular 2 Pipe - filter by JSON key

  15. 15

    Angular 2 pipe/filter with component variable

  16. 16

    Angular 2 pipe/filter with component variable

  17. 17

    angular 2 Pipe - filter by JSON key

  18. 18

    Angular 2 pipe to filter grouped arrays

  19. 19

    Angular2 filter pipe on input

  20. 20

    Angular2: pass in a property name to a pipe

  21. 21

    Filter an array of objects by a property containing a substring in AngularJS

  22. 22

    _.filter array of objects using a property with either value

  23. 23

    Filter array of objects where object property is date

  24. 24

    Filter by text search in some fields of array of objects

  25. 25

    angularjs custom filter for search in an Array of objects

  26. 26

    How to search by a property in array of objects in php?

  27. 27

    Can't use .filter() nor for on an array of objects in my pipe

  28. 28

    lodash filter array of objects by array Angular 5

  29. 29

    Angular2 filter/pipe using radio buttons to filter

HotTag

Archive