Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

257
Views
Filtering a list of objects by a child array in Angular 2

I have data that looks like this

[
  {id: 1234,
   Name: 'John',
   Tags: ['tag1', 'tag2']
  },
  {id: 1235,
   Name: 'Mike',
   Tags: ['tag1', 'tag3']
  }
]

I want to be able to type into a search bar and filter the data to search for related tags. There was a built in filter pipe for this in angular 1 but it looks like it has been removed in angular 2. I've been looking into custom pipes but the only way I can think to do it is with a nested loop that loops over all of the objects then loops through the tags. Am I thinking about this wrong. Is there an easier way to do this or a built in function that would work for this?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

You can just use normal javascript APIs to get that behaviour:

data = [
  {id: 1234,
   Name: 'John',
   Tags: ['tag1', 'tag2']
  },
  {id: 1235,
   Name: 'Mike',
   Tags: ['tag1', 'tag3']
  }
];

filterDataByTag(searchTerm: string) {

   // filter the data array, based on some condition
   return this.data.filter(item => {

      // only include an item in the filtered results
      // if that item's Tags property includes the searchTerm
      // includes is a built in array method in ES2016
      return item.Tags.includes(searchTerm);
   });  
}

In my example, i'm harding coding the data, but you can adjust to suit your situation. The key point is the function returns a filtered list of the data based on the searchTerm, so you can just call this method each time you want to refresh your filtered list (for eg on the input event of your search field)

over 4 years ago · Santiago Trujillo Report

0

You should reorganize data into a reverse index store :

export interface StoreData {
  Tag: string;
  Peoples: People[] = [];
}

const store: StoreData[] = [];

export interface People {
  id: number; 
  Name: string;
  Tags: string[];
}

loadPeopleStore(peoples: People) {
  peoples.forEach(p => {
    p.Tags.forEach(t => {
      let storeData = store.filter(d => d.Tag === t);
      if(storeData.length == 1) {
        storeData[0].Peoples.push(p);
      } else {
        store.push({Tag: t, Peoples[p]});
      }
    }
  }
}

initYourPipe() {
 let peoples: People[] =   [
    {id: 1234,
     Name: 'John',
     Tags: ['tag1', 'tag2']
    },
    {id: 1235,
     Name: 'Mike',
     Tags: ['tag1', 'tag3']
    }
  ]

  this.loadPeopleStore(peoples);


}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!