JS通过多个参数过滤

时间:2019-07-17 08:35:32

标签: javascript arrays

我有这个items数组和fitlerParams数组。

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

我想要的是使用items过滤fitlerParams数组 如果fitlerParams数组只有一个元素,我会很容易地这样做

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

基本上我所做的是,获取params字符串并用“ =”分割,然后检查param的第一部分等于area,第二部分等于operation。我的问题是该数组中有多个过滤参数,我想检查所有这些参数。

如何使用JS实现此目标? 任何帮助!

非常感谢。 =)

3 个答案:

答案 0 :(得分:2)

检查传递的参数中的.some拆分时是否匹配areaoperation属性:

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);

如果您只希望为每个项目拆分一次,则可以提前这样做:

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);

答案 1 :(得分:1)

对于true数组中的任何值,您可以使用.some(...)检查该条件是否为filterParams。这是一个示例:

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);

答案 2 :(得分:1)

您可以生成一个拆分值数组,并检查是否使用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);