I'm trying to let the user filter this array
const allProjects= [
{
id:1,
name: 'Project A',
comments: ['a','b'],
tests: [
{id:1, name:'test1', comments:['aa','bb']},
{id:2, name:'abc', comments:['aa','bb']},
{id:2, name: 'test2', comments: ['a','b']}
]
},
{
id:2,
name: 'Project B',
comments: ['project comment1','project comment2'],
tests: [
{id:1, name:'test1', comments:['aa','bb']},
{id:2, name:'abc', comments:['aa','bb']},
{id:2, name: 'test2', comments: ['a','b']}
]
}
]
What is the correct way to filter by variable options?
the options to filter by: project.name, project.comment, test.name, test.comment
for example filter by:
project.comment and test.comment
or by project.comment only
or test.name only
etc..
Code currently: Filter only by test name:
const k = keyword.toLowerCase();
let filtered;
filtered = allProjects.map(x => Object.assign({}, x));
filtered = filtered.filter((project) => {
project.tests = project.tests.filter(
(test)=> test.name.toLowerCase().includes(k));
return project.tests.length>0
});
return filtered