如何将字符串文字传递到 javascript 中的过滤器函数

How to pass string literal into a filter function in javascript

我想将字符串文字传递给过滤函数。

结果应该是

filter = data.filter(o => o.tag.toLowerCase().indexOf(("website").toLowerCase()) != -1 &&
          o.tag.toLowerCase().indexOf(("phone").toLowerCase()) != -1

我目前正在做的是

for (let i = 0; i < values.length; i++) {
          if (i + 1 >= values.length) {
            query = query.concat(` o.tag.toLowerCase().indexOf(("${values[i]}").toLowerCase()) != -1 `);
          } else {
            query = query.concat(` o.tag.toLowerCase().indexOf(("${values[i]}").toLowerCase()) != -1 && `);
          }

        }
let filter = data.filter(o => `${query}`);
        debugger;


我要过滤的示例片段。 我想让这个过滤函数成为动态的

data=[{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}

]

let filter=[];
filter=data.filter(o=>o.tag.indexOf("logo")!=-1 && o.tag.indexOf("abc")!=-1)

console.log(filter);

为什么不能将 for 放入过滤器回调函数中?

例如:

data = data.filter( o => {
  for ( let i = 0; i < values.length; i++ ) {
    if ( o.tag.toLowerCase().indexOf(values[i].toLowerCase()) == -1 ) {
       return false;
    }
  }
  return true;
} );

如果您想确保 values 所有 匹配 tag 中的项目,那么您可以使用 Array#every做吧。

const arr1 = ["aardvark", "ant", "alien"];
const arr2 = ["apple", "audi", "bcoccoli"];

const startsWithA = word => word[0] === "a";

console.log(arr1.every(startsWithA));
console.log(arr2.every(startsWithA));

此外,可以使用 String#includes 方法代替 .indexOf,因为它直接 returns 一个布尔值。

const word = "applesauce";

console.log(word.includes("apple"));
console.log(word.includes("sauce"));

所以我们得到这个:

const data = [{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}]

const values = ["logo", "abc"];

let result = data
  .filter(
    ({tag}) => values.every(
        value => tag.toLowerCase()
            .includes(value.toLowerCase())
     ) 
  )
  
console.log(result);

但是,由于 tag 包含逗号分隔列表,您可能会得到误报:

const tag1 = "foobar";
const tag2 = "foo,bar";

const values = ["foo", "bar"];

console.log(values.every(value => tag1.includes(value)));
console.log(values.every(value => tag2.includes(value)));

一个标签可能是由几个其他词组成并匹配,而你只想做一个完整的匹配。您可以使用 String#split 来分隔标签,您只需查找两个数组是否重叠:

const tag1 = "foobar";
const tag2 = "foo,bar";

const values = ["foo", "bar"];

const arrayOfTag1 = tag1.split(",");
const arrayOfTag2 = tag2.split(",");

console.log(values.every(value => arrayOfTag1.includes(value)));
console.log(values.every(value => arrayOfTag2.includes(value)));

Sets 可用于降低整个查找的复杂性,在这种情况下,过滤可能如下所示:

const data = [{name:"fpl.xlsx",author:"hello",hits:6,date:"2020-01-01",tag:"logo,website"}
,{name:"corporate.pptx",author:"hellob",hits:1,date:"2020-02-01",tag:"logo"},
{name:"index.html",author:"hellob",hits:7,date:"2020-02-02",tag:"logo,abc"}]

const values = ["logo", "abc"];

let result = data
  .filter(
    ({tag}) => {
      const lookupTags = new Set(
        tag
          .split(",")
          .map(x => x.toLowerCase())
      );
    
      return values.every(value => lookupTags.has(value.toLowerCase())
     )
   })
  
console.log(result);