1

I`m trying to filter an array but I don t succeed to filter array inside the object

I will give us an example of how the array is:

const data = [
   {
    path: 'data1',
    main: [
       {
        path: 'mainData11'
       },
       {
        path: 'mainData12'
       }
     ]
   },
   {
    path: 'data2',
    main: [
       {
        path: 'mainData21'
       },
       {
        path: 'mainData22'
       }
     ]
   }
];

const filterArray = ['data1', 'mainData12'];

expected result

const data = [
 {
  path: 'data1'
  main: [
   {
    path: 'mainData12' 
   }
  ]
 }
]

What I`ve tried

data.filter(el => filterArray.includes(el.path))

I did not succeed to filter the main inside object...

How I do that?

Thanks!

**UPDATE -- CURRENT SOLUTION

data.reduce((results, item) => {
 if(filterArray.some(f => item.path === f)){
  results.push(
   {
    ...item,
    path: item.path,
    main: item.main.filter(i => filterArray.some(f => i.path === f))
   }
  )
 };
 return results;
}, []);
0

2 Answers 2

1

You can solve it by rebuilding the object:

const data = [{
    path: 'data1',
    main: [{
        path: 'mainData11',
      },
      {
        path: 'mainData12',
      },
    ],
  },
  {
    path: 'data2',
    main: [{
        path: 'mainData21',
      },
      {
        path: 'mainData22',
      },
    ],
  },
];

const filterArray = ['data1', 'mainData12'];

const filteredData = data
  .filter(entry => entry.path === filterArray[0])
  .map(entry => ({
    path: entry.path,
    main: entry.main.filter(x => x.path === filterArray[1]),
  }))
  .filter(entry => entry.main.length);

console.log(filteredData);

Sign up to request clarification or add additional context in comments.

2 Comments

thanks. Yes this is the current solution to rebuild the object.
A filter followed by a map should be done in a single iteration with a reduce or else you are iterating the collection multiple times.
0

This can be done in one step if you use a reduce

const data = [
   {
    path: 'data1',
    main: [
       {
        path: 'mainData11'
       },
       {
        path: 'mainData12'
       }
     ]
   },
   {
    path: 'data2',
    main: [
       {
        path: 'mainData21'
       },
       {
        path: 'mainData22'
       }
     ]
   }
];

const filterArray = ['data1', 'mainData12'];

const results = data.reduce((results, item) => {
  if (filterArray.some(f => item.path === f) && item.main.some(i => filterArray.some(f => i.path === f))) {
    results.push(
      { path: item.path, main: item.main.filter(i => filterArray.some(f => i.path === f)) }
    );
  }
  return results;
 }, []);

console.log(results)

2 Comments

thanks for awsering. The problem is that the filterArray elements position are not fixed. Exemple data1 can be on positon 3. I must to search in that array if exists
Edited to search filter array

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.