1

I'm trying to filter one array so that the objects will display if they match one of the options at the filterOptions (another array).

Example: If filterOptions contains the string big it should show the objects with sizes containing the string big

However, I'm not sure how to split data.sizes correctly so it can use this data to filter with, how can I do this?

https://codesandbox.io/s/thirsty-ellis-dplhg

export const App = () => {
  const data = [
    {sizes: 'big,small',},
    {sizes: 'medium,small'},
    {sizes: 'small,big',},
    {sizes: 'big',},
    {sizes: 'medium',}
  ];

  const filterOptions = ['big', 'small'];
  const splitData = data.map(items => items.sizes.split(','));

  return(
    <div>
      {console.log(splitData)}
      {data.filter(items => items.sizes.includes(filterOptions))
           .map(item => <div>{item.sizes}<br /></div>)}
    </div>
  )
}
2
  • So in this case only show sizes containing both big and small, or does sizes: 'big' pass even though 'small' isn't in it? Commented Jun 5, 2019 at 20:31
  • my two cents suggestion: sizes should be an array. sizes: ['big', 'small', 'medium']. In this way, you don't have to split it and it is clearer. Not to mention, sizes: 'small, space__big, __space __space__medium__space____space' will break your code if you don't split and trim. Commented Jun 5, 2019 at 20:40

4 Answers 4

2

this answer can fit as many filter options as possible

data.filter(items => items.sizes.split(',').find(size => filterOptions.includes(size)))
Sign up to request clarification or add additional context in comments.

Comments

1

I believe you are looking for:

{data.filter(i => i.sizes.split(",").some(size => filterOptions.includes(size)))
     .map(item => <div>{item.sizes}<br/></div>)}

Example of the filter result:

const data = [
  {sizes: 'big,small'},
  {sizes: 'medium,small'},
  {sizes: 'small,big'},
  {sizes: 'big'},
  {sizes: 'medium'}
];

const filterOptions = ['big','small'];

let res = data.filter(
    i => i.sizes.split(",").some(
        size => filterOptions.includes(size)
    )
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

However, for your particular set of sizes ("small", "big" and "medium"), where there are no string that is also substring of another string, you can go with the next filter using a regular expression constructed from the filterOptions array:

const data = [
  {sizes: 'big,small'},
  {sizes: 'medium,small'},
  {sizes: 'small,big'},
  {sizes: 'big'},
  {sizes: 'medium'}
];

const filterOptions = ['big','small'];

let res = data.filter(
    ({sizes}) => new RegExp(filterOptions.join("|")).test(sizes)
);

console.log(res);
.as-console {background-color:black !important; color:lime;}
.as-console-wrapper {max-height:100% !important; top:0;}

Or, in your context of code:

{data.filter(({sizes}) => new RegExp(filterOptions.join("|")).test(sizes))
     .map(item => <div>{item.sizes}<br/></div>)}

Comments

0

Here's how you might go about it (there are a lot of different ways to achieve your result though):

const data = [
  { sizes: 'big,small', },
  { sizes: 'medium,small' },
  { sizes: 'small,big', },
  { sizes: 'big', },
  { sizes: 'medium', }
];

const filterOptions = [ "big", "small" ];
const filterData = data.filter( ( { sizes } ) => {
  for ( const filter of filterOptions ) {
    if ( sizes.includes( filter ) ) {
      return true;
    }
  }
} );

console.log( filterData );

This is assuming of course you don't want the data sizes to contain both filterOptions.

Comments

0

Are you looking for

const data = [
    {
      sizes: 'big,small',
    },
    {
      sizes: 'medium,small'
    },
    {
      sizes: 'small,big',
    },
    {
      sizes: 'big',
    },
    {
      sizes: 'medium',
    }
  ];

const filterOptions = ['big','small'];

const filterData = data.filter( d => filterOptions.every(f => d.sizes.split(',').includes(f)));

console.log(filterData);

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.