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
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.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:
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. 🙂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:
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:
Example Usage:
Hope this helps?
Let me know if you have any questions…