0

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

2 Answers 2

2

I'd say something like this:

const filteredProjects = this.allProjects
  .filter(project => filterByName ? project.name.toLowerCase().includes(k) : true)
  .filter(project => filterByTestName ? project.tests.some(test => test.name.toLowerCase().includes(k)) : true)
  .filter(/***/);
Sign up to request clarification or add additional context in comments.

Comments

1

My suggestion is to create different compare functions to each field and add it to the compareFn props of the filtering object. They return boolean based on the search condition fits the record or not. (I did not implement the compareFns.) In the below example the user is searching the t1 string.

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']}
             ]
       }

   ]
   
  const filtering = [
   {
      accessor: 'comments',
      searchValue: 't1',
      compareFn: (searchValue, record) => true
   },
   {
      accessor: 'tests.name',
      searchValue: 't1',
      compareFn: (searchValue, record) => true
    }
  ]
   
  const filterFn = (record) => (
    filtering.reduce((acc, { searchValue, compareFn }) => (
      acc && compareFn(searchValue, record)
    ),true)
  )
     
  console.log(allProjects.filter(filterFn))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.