skip to Main Content

If you have an array that must be filtered based on anther array value.

The list that must be filtered

[
  { name: 'Anna', age: 23, skills: [{name: 'a', desc: 'aaa'}] },
  { name: 'Barbara', age: 26, skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'} ] }
]

input for comparison/filter

   [{name: 'b', desc: 'bbb'}]

function called with input [{name: ‘b’, dec: ‘bbb’}]

updatePersonList([{name: 'b', dec: 'bbb'}]);

updatePeronList(skill: {name: string, desc: string}) {
this.filteredList = this.filteredList.filter(person => {
  person?.skills(skills => {
    // I have tried include, some and some other things on I think its because you cannot compare objects like that that must be done with .equels or some other way.
  });
});
}

What I like to to is if there is any object in the given array that is also available in the skill array than this person must be shown in the filtered list.

If there is no match at al the person must not be shown.

Maybe its’ still unclear please ask a question and I will try the explain in another way

4

Answers


  1. We can use the array operation some to check if a condition is met atleast once, we run this for two times, one for the skills array and another for the data inside the lookup array.

    const arr = [
      { name: 'Anna', age: 23, skills: [{name: 'a', desc: 'aaa'}] },
      { name: 'Barbara', age: 26, skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'} ] }
    ]
    
    const lookup = [{name: 'b', desc: 'bbb'}]
    
    function getFiltered(lookup) {
      return arr.filter((item) => {
        return item.skills.some((data) => lookup.some((lookupItem) => lookupItem.name === data.name && lookupItem.desc === data.desc));
      });
    }
    
    console.log(getFiltered(lookup));
    Login or Signup to reply.
  2. so if I understood correctly, you are trying to filter each object based on their skills. You are working on three data types: an array of objects, and inside each object you have another array of objects which you want to filter. To achieve the desired result, I would suggest the following:

    function filterBySkills(inputForComparison) {
        return unfilteredArray.filter(person => {
            return person.skills.find(skill => {
                return JSON.stringify(skill) === JSON.stringify(inputForComparison);
            });
        });
    }
    

    where inputForComparison = {name: 'b', desc: 'bbb'} . I have chosen turning the objects into strings to compare them, this can also be done using lodash _.isEqual() as mentioned here. Hope this can help you solve the issue. 🙂

    Login or Signup to reply.
  3. To simplify the code, you can avoid the nested .some() method by using a more straightforward approach, like combining the lookup objects in a Set or filtering directly with a cleaner structure. Here’s a more concise version of your code:

    const arr = [
      { name: 'Anna', age: 23, skills: [{name: 'a', desc: 'aaa'}] },
      { name: 'Barbara', age: 26, skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'} ] }
    ];
    
    const lookup = [{name: 'b', desc: 'bbb'}];
    
    function getFiltered(lookup) {
      const lookupSet = new Set(lookup.map(item => JSON.stringify(item)));
      console.log(lookupSet);
      return arr.filter(item =>
        item.skills.some(skill => lookupSet.has(JSON.stringify(skill)))
      );
    }
    
    console.log(getFiltered(lookup));
    Login or Signup to reply.
  4. From what I understand, you want to filter a list of persons based on whether they have any skills that match a given set of skills to filter.

    Use a combination of filter() and some() to check for skill matches:

    updatePersonList(skillsToFilter: {name: string, desc: string}[]): void {
      this.filteredList = this.originalList.filter(person => 
        person.skills.some(personSkill => 
          skillsToFilter.some(filterSkill => 
            this.areSkillsEqual(personSkill, filterSkill)
          )
        )
      );
    }
    
    // Helper method to compare skills precisely
    private areSkillsEqual(
      skill1: {name: string, desc: string}, 
      skill2: {name: string, desc: string}
    ): boolean {
      return skill1.name === skill2.name && skill1.desc === skill2.desc;
    }
    

    Example Usage:

    riginalList = [
      { name: 'Anna', skills: [{name: 'a', desc: 'aaa'}] },
      { name: 'Barbara', skills: [{name: 'a', desc: 'aaa'}, {name: 'b', desc: 'bbb'}] }
    ];
    
    // Filter for persons with skill 'b'
    const skillsToFilter = [{name: 'b', desc: 'bbb'}];
    this.updatePersonList(skillsToFilter);
    

    Hope this helps?
    Let me know if you have any questions…

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search