当前位置: 代码迷 >> JavaScript >> 如何使用多个文本字符串条件从数组中过滤出多个对象
  详细解决方案

如何使用多个文本字符串条件从数组中过滤出多个对象

热度:33   发布时间:2023-06-12 14:08:35.0

我正在尝试编写测试,并且我有一个对象数组,看起来像这样:

menuOfProducts: [ 
  { text: 'Product One',
    selector: '#product-one #productone',
    path: 'productone' },
  { text: 'Product Two',
    selector: '#product-two #producttwo',
    path: 'shop/catalog/producttwo' },
  { text: 'Product Three',
    selector: '#product-three #productthree',
    path: 'shop/catalog/productthree' },
  { text: 'Product Four',
    selector: '#product-four #productfour',
    path: 'shop/catalog/productfour' },
  { text: 'Product Five',
    selector: '#product-five #productfive',
    path: 'shop/catalog/productfive' }
]

我想做的是过滤掉几个对象并返回其余的对象。

到目前为止,我已经尝试使用.filter()过滤掉运行正常的对象之一。 但是,可能需要按文本过滤出多个产品。 这就是我现在所拥有的:

if (environment === 'test') {
  menuOfProducts = menuOfProducts.filter(function (option) {
    return option.text !== 'Product Two';
  });
}

并使用此过滤器,我得到了减去“乘积二”的正确数组:

[ 
  { text: 'Product One',
    selector: '#product-one #productone',
    path: 'productone' },
  { text: 'Product Three',
    selector: '#product-three #productthree',
    path: 'shop/catalog/productthree' },
  { text: 'Product Four',
    selector: '#product-four #productfour',
    path: 'shop/catalog/productfour' },
  { text: 'Product Five',
    selector: '#product-five #productfive',
    path: 'shop/catalog/productfive' }
]

但是如上所述,我现在想按文本过滤出多个对象。 并且想知道我该如何处理? 我试图在过滤器中传递另一个条件,如下所示:

if (environment === 'test') {
  menuOfProducts = menuOfProducts.filter(function (option) {
    return option.text !== 'Product Two' || 'Product Three';
  });
}

但是然后我得到了返回了ALL对象的数组,并且没有任何内容被过滤掉。 任何帮助将不胜感激。 提前谢谢了

您将获得所有返回的值,因为'Product Three'是值

像这样使用运算符:

if (environment === 'test') {
  menuOfProducts = menuOfProducts.filter(function (option) {
    return option.text !== 'Product Two' && option.text !== 'Product Three';
  });
}

如果您有多个option.textfilter ,则可以创建这些值的数组并使用 :

if (environment === 'test') {
  menuOfProducts = menuOfProducts.filter(function(option) {
    return !['Product Two', 'Product Three'].includes(option.text);
  });
}

您需要这样做:

if (environment === 'test') {
  menuOfProducts = menuOfProducts.filter(function (option) {
    return option.text !== 'Product Two' && option.text !== 'Product Three';
  });
}

您使用的逻辑运算符错误。 您以一种真实的人可能会理解的方式来使用它,但是必须区别对待计算机。

您的逻辑术语: option.text !== 'Product Two' || 'Product Three'; option.text !== 'Product Two' || 'Product Three';
我把它缩短为A !== B || C A !== B || C
使其等于: (A !== B) || C (A !== B) || C
这就是“ A不等于B,其他C”
因此,如果A !== B为true,则整个项的值为true(因为true || anything始终为true )。 但是,如果A !== B为假,则整个项将求值为V (因为false || anything始终是anything ),所以您将拥有C true值, C是字符串'Product Three' ,这是一个值。
最后,您的完整期限option.text !== 'Product Two' || 'Product Three' option.text !== 'Product Two' || 'Product Three'将永远是正确的,因此不会滤除任何东西

您需要的是A !== B && A !== C ,其计算方式类似于(A !== B) && (A !== C)
在这种情况下,该术语将仅是true id option.text既不等于'Product Two'也不等于'Product Three'
因此,您的术语必须是option.text !== 'Product Two' && option.text !== 'Product Three'

 console.clear() menuOfProducts = [ { text: 'Product One', selector: '#product-one #productone', path: 'productone' }, { text: 'Product Two', selector: '#product-two #producttwo', path: 'shop/catalog/producttwo' }, { text: 'Product Three', selector: '#product-three #productthree', path: 'shop/catalog/productthree' }, { text: 'Product Four', selector: '#product-four #productfour', path: 'shop/catalog/productfour' }, { text: 'Product Five', selector: '#product-five #productfive', path: 'shop/catalog/productfive' } ] menuOfProducts = menuOfProducts.filter(function (option) { return option.text !== 'Product Two' && option.text !== 'Product Three'; }); console.log(menuOfProducts) 

什么是运算符优先级

MDN文档说:

运算符优先级确定彼此之间解析运算符的方式。 具有较高优先级的运算符将成为具有较低优先级的运算符的操作数。

这意味着我们具有所有可能的运算符的排序列表,并且该列表中较高的运算符将在较低的运算符之前进行解析。
我们可以使用括号[ () ]对其进行可视化,它们是分组运算符,位于运算符优先级列表的顶部。

一个例子A = B + C
就像A = (B + C) ,因为赋值运算符(single = )的优先级为3,加法运算符( + )的优先级为13,因此在=之前进行解析。

您的条件是A !== B || C A !== B || C 让我们看一下优先级列表

| precedence | operator name     | operator |
| 10         | Strict Inequality |   !==    |
|  5         | Logical OR        |   ||     |

严格不等式运算符的优先级高于逻辑或运算符。 所以A !== B || C A !== B || C类似于(A !== B) || C (A !== B) || C所有运营商

  相关解决方案