1

I have this items array and a filterParams array.

const items = [
  {
    count: 1,
    operation: 'Change Status',
    area: 'dish_names',
    days: '2019-07-17'
  },
  {
    count: 14,
    operation: 'Create',
    area: 'dishes',
    days: '2019-07-17'
  },
  {
    count: 23,
    operation: 'Create',
    area: 'dish_names',
    days: '2019-07-17'
  },
  { count: 1, operation: 'Delete', area: 'dishes', days: '2019-07-17' },
  {
    count: 2,
    operation: 'Update',
    area: 'dish_names',
    days: '2019-07-17'
  }
]

const filterParams = [
  'restaurant_menu_masters=Update',
  'dish_categories=Update',
  'dishes=Create'
]

What I want is to filter the items array using filterParams.

If filterParams array only has one element, I would easily do it like this

const data = items.filter(Element=> {
  let params = filterParams[0].split("=");
  return Element.area === params[0] && Element.operation === params[1]
})

Basically What I did was, take the params string and split it by "=" and then check first part of the param equals to area and the second to operation. My problem is it has more than one filter parameters in that array and I want to check all of that.

How do I achieve this using JS?

Any help!

Thanks in advance =)

3 Answers 3

2

Check if .some of the passed parameters, when split, match the area and operation properties:

const items=[{count:1,operation:"Change Status",area:"dish_names",days:"2019-07-17"},{count:14,operation:"Create",area:"dishes",days:"2019-07-17"},{count:23,operation:"Create",area:"dish_names",days:"2019-07-17"},{count:1,operation:"Delete",area:"dishes",days:"2019-07-17"},{count:2,operation:"Update",area:"dish_names",days:"2019-07-17"}];

const fitlerParams = [
  'restaurant_menu_masters=Update',
  'dish_categories=Update',
  'dishes=Create'
]

const filtered = items.filter(
  ({ area, operation }) => fitlerParams.some((str) => {
    const [aFind, oFind] = str.split('=');
    return area === aFind && operation === oFind;
  })
);
console.log(filtered);

If you want to only split once for each item, you can do so in advance:

const items=[{count:1,operation:"Change Status",area:"dish_names",days:"2019-07-17"},{count:14,operation:"Create",area:"dishes",days:"2019-07-17"},{count:23,operation:"Create",area:"dish_names",days:"2019-07-17"},{count:1,operation:"Delete",area:"dishes",days:"2019-07-17"},{count:2,operation:"Update",area:"dish_names",days:"2019-07-17"}];

const fitlerParams = [
  'restaurant_menu_masters=Update',
  'dish_categories=Update',
  'dishes=Create'
];
const splitParams = fitlerParams.map(str => str.split('='));

const filtered = items.filter(
  ({ area, operation }) => splitParams.some(([aFind, oFind]) => {
    return area === aFind && operation === oFind;
  })
);
console.log(filtered);

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

Comments

1

You can use .some(...) to check if that condition is true for any of the values in the filterParams array. Here is an example:

const items = [{
    count: 1,
    operation: 'Change Status',
    area: 'dish_names',
    days: '2019-07-17'
  },
  {
    count: 14,
    operation: 'Create',
    area: 'dishes',
    days: '2019-07-17'
  },
  {
    count: 23,
    operation: 'Create',
    area: 'dish_names',
    days: '2019-07-17'
  },
  {
    count: 1,
    operation: 'Delete',
    area: 'dishes',
    days: '2019-07-17'
  },
  {
    count: 2,
    operation: 'Update',
    area: 'dish_names',
    days: '2019-07-17'
  }
]

const fitlerParams = [
  'restaurant_menu_masters=Update',
  'dish_categories=Update',
  'dishes=Create'
]

const data = items.filter(Element => {
  return fitlerParams.some(value => {
    let params = value.split("=");
    return Element.area === params[0] && Element.operation === params[1]
  });
});

console.log(data);

Comments

1

You could generate an array of splitted values and check if you found one true condition with some.

const
    items = [{ count: 1, operation: 'Change Status', area: 'dish_names', days: '2019-07-17' }, { count: 14, operation: 'Create', area: 'dishes', days: '2019-07-17' }, { count: 23, operation: 'Create', area: 'dish_names', days: '2019-07-17' }, { count: 1, operation: 'Delete', area: 'dishes', days: '2019-07-17' }, { count: 2, operation: 'Update', area: 'dish_names', days: '2019-07-17' }],
    filterParams = ['restaurant_menu_masters=Update', 'dish_categories=Update', 'dishes=Create'],
    filters = filterParams.map(s => s.split('=')),
    data = items.filter(o => filters.some(([area, operation]) =>
        o.area === area && o.operation === operation));

console.log(data);

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.