考虑到被过滤的选项是可选的,过滤一个数组。

问题描述 投票:1回答:2

我有一段代码,应该根据一些条件来过滤数组。一次可以是1个条件,也可以是2个条件,取决于有多少用户被插入。

const arr = [
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
  {
    name: 'Bill',
    age: 47,
    old: 'No'
  },
]
const filteredData = [4, "John", "No"];

var find = arr.filter(function(result) {
  return filteredData.includes(result.name) && filteredData.includes(result.age) && filteredData.includes(result.old);
})

console.log(find)

在这种情况下,代码可以工作,但是如果我只添加一个值在 const filteredData = [4, "John", "No" ];,喜欢。const filteredData = ["John"];,我应该得到的。

[
  {
    name: 'John',
    age: 44,
    old: 'No'
  },
  {
    name: 'John',
    age: 4,
    old: 'No'
  },
]

...但现在我什么都没有得到,因为在... ...我应该得到: filteredData 3个条件都应该是强制性的。如何修改代码,使条件的数量成为可选的?

javascript
2个回答
2
投票

那这个呢?

用一个对象作为过滤器来表达你要过滤的东西,这样就比较清楚了。如果你只通过 name: 'John':

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name)) 
  && (!filteredData.ages || filteredData.ages.includes(result.age)) 
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})
       
console.log(find)

如果你也通过了年龄。

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = { names: ["John"], ages: [4] };

var find = arr.filter(function(result) {
return (!filteredData.names || filteredData.names.includes(result.name)) 
  && (!filteredData.ages || filteredData.ages.includes(result.age)) 
  && (!filteredData.olds || filteredData.olds.includes(result.old));
})
       
console.log(find)

另外,这样你可以同时过滤多个名字(或年龄)。如果你每次只关心每个字段的一个值,你可以只使用一个没有数组的普通对象,并相应地调整代码。

{ name: "John", age: 4 }

在任何情况下,重要的是你能区分不同的字段(姓名,年龄,年龄),否则可能会导致搜索时出现意外行为(因为你无法分辨哪些值是独占的,哪些是包含的),就像你已经面对的那样。


0
投票

也许我可以帮助你。

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]
const filteredData = ["John"];

const newArr = arr.filter((it) => !filteredData.indexOf(it.name))

display.log(JSON.stringify(newArr))

现在你可以看到这个由名字John组成的对象。


0
投票

你需要做的是 filteredData 作为一个对象,这样你就可以知道你要检查的是什么字段。

const arr = [
  {
    name:'John',
    age:44,
    old:'No'
  },
   {
    name:'John',
    age:4,
    old:'No'
  },
  {
    name:'Bill',
    age:47,
    old:'No'
  },
]

// Make this as object
let filteredData = {
    name:'John',
    age:4,
    old:'No'
  };

var find = arr.filter(function(result) {
  // Get all key from filteredData
  const filteredDataKeys = Object.keys(filteredData)
  // Check if all key pass
  return filteredDataKeys.every(key => filteredData[key] === result[key])
})
       
console.log(find)

 filteredData = {
    name:'John',
  };

find = arr.filter(function(result) {
  // Get all key from filteredData
  const filteredDataKeys = Object.keys(filteredData)
  // Check if all key pass
  return filteredDataKeys.every(key => filteredData[key] === result[key])
})

console.log(find)
© www.soinside.com 2019 - 2024. All rights reserved.