如何在js中使用带有箭头功能的2个条件使用对象的过滤器数组? [英] How to use filter array of objects by 2 conditions with an arrow function in js?

查看:120
本文介绍了如何在js中使用带有箭头功能的2个条件使用对象的过滤器数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个像这样的数组:

Suppose I have an array like:

  const items=[{
        "taskType": "type2",
        "taskName": "two",
        "id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
      },
      {
        "taskType": "type1",
        "taskName": "two",
        "id": "c5385595-2104-409d-a676-c1b57346f63e"
      }]

我想拥有一个箭头(过滤器)功能,该功能返回除taskType = type2和taskName = two之外的所有项目.因此,在这种情况下,它仅返回第二项?

I want to have an arrow (filter) function that returns all items except for where taskType=type2 and taskName=two. So in this case it just returns the second item?

推荐答案

您可以尝试在

You can try negating the condition in Array.prototype.filter()

const items=[{
        "taskType": "type2",
        "taskName": "two",
        "id": "19d0da63-dfd0-4c00-a13a-cc822fc81298"
      },
      {
        "taskType": "type1",
        "taskName": "two",
        "id": "c5385595-2104-409d-a676-c1b57346f63e"
      }]

var res = items.filter(task => !(task.taskType == 'type2' && task.taskName == 'two'));

console.log(res);

这篇关于如何在js中使用带有箭头功能的2个条件使用对象的过滤器数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆