在Typescript中使用多个条件筛选数组。

huangapple go评论131阅读模式
英文:

Filter array with multiple conditions in Typescript

问题

我想在Typescript中创建一个具有多个条件的筛选器。假设我有以下数组:

{
    "id": 1513,
    "procedure": {
        "title": "Sample Procedure 1"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "OUT OF DEADLINE"
    }
},
{
    "id": 1514,
    "procedure": {
        "title": "Sample Procedure 2"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "SUBMITTED"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
}

我希望用户能够使用多个选项来筛选这个数组,例如:筛选截止日期为"WITHIN DEADLINE"且状态为"IN PROCESS"的流程。

编辑:我需要用户选择的条件来自于请求主体,并根据这些条件生成新的筛选数组。

英文:

I want to create a filter with multiple conditions in Typescript. Let's suposse I have this array:

{
    "id": 1513,
    "procedure": {
        "title": "Sample Procedure 1"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "OUT OF DEADLINE"
    }
},
{
    "id": 1514,
    "procedure": {
        "title": "Sample Procedure 2"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "SUBMITTED"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
},

I want the user can filter this array using multiple options, for example: Filter the procedures with deadline in "WITHIN DEADLINE" and status in "IN PROCESS".

Edit: I require the user-selected conditions to come from the body, and based on these conditions, generate the new filtered array

答案1

得分: 0

const arr = [{
    "id": 1513,
    "procedure": {
        "title": "Sample Procedure 1"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "OUT OF DEADLINE"
    }
},
{
    "id": 1514,
    "procedure": {
        "title": "Sample Procedure 2"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "SUBMITTED"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
},
{
    "id": 1515,
    "procedure": {
        "title": "Sample Procedure 3"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
}]

const result = arr.filter(item => item.status.status === 'IN PROCESS' 
    && item.deadline.deadline === 'WITHIN DEADLINE')
console.log(result)

Output:

[
  {
    id: 1515,
    procedure: { title: 'Sample Procedure 3' },
    category: { title: 'Press' },
    status: { status: 'IN PROCESS' },
    deadline: { deadline: 'WITHIN DEADLINE' }
  }
]

For TypeORM, you can add an OR condition to an existing WHERE expression as follows:

createQueryBuilder("entity")
    .where("entity.status = :status", { status: "IN PROCESS" })
    .orWhere("entity.deadline = :deadline", { deadline: "WITHIN DEADLINE" })

Reference link

英文:
const arr = [{
    "id": 1513,
    "procedure": {
        "title": "Sample Procedure 1"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "OUT OF DEADLINE"
    }
},
{
    "id": 1514,
    "procedure": {
        "title": "Sample Procedure 2"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "SUBMITTED"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
},
{
    "id": 1515,
    "procedure": {
        "title": "Sample Procedure 3"
    },
    "category": {
        "title": "Press"
    },
    "status": {
        "status": "IN PROCESS"
    },
    "deadline": {
        "deadline": "WITHIN DEADLINE"
    }
},]

const result = arr.filter(item => item.status.status === 'IN PROCESS' 
    && item.deadline.deadline === 'WITHIN DEADLINE')
console.log(result)

Output:

[
  {
    id: 1515,
    procedure: { title: 'Sample Procedure 3' },
    category: { title: 'Press' },
    status: { status: 'IN PROCESS' },
    deadline: { deadline: 'WITHIN DEADLINE' }
  }
]

Edit:

For typeorm you can add OR into an existing WHERE expression:

createQueryBuilder("entity")
    .where("entity.status = :status", { status: "IN PROCESS" })
    .orWhere("entity.deadline = :deadline", { deadline: "WITHIN DEADLINE" })

https://typeorm.io/select-query-builder#adding-where-expression

huangapple
  • 本文由 发表于 2023年6月13日 02:12:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/76459265.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定