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

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

Filter array with multiple conditions in Typescript

问题

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

  1. {
  2. "id": 1513,
  3. "procedure": {
  4. "title": "Sample Procedure 1"
  5. },
  6. "category": {
  7. "title": "Press"
  8. },
  9. "status": {
  10. "status": "IN PROCESS"
  11. },
  12. "deadline": {
  13. "deadline": "OUT OF DEADLINE"
  14. }
  15. },
  16. {
  17. "id": 1514,
  18. "procedure": {
  19. "title": "Sample Procedure 2"
  20. },
  21. "category": {
  22. "title": "Press"
  23. },
  24. "status": {
  25. "status": "SUBMITTED"
  26. },
  27. "deadline": {
  28. "deadline": "WITHIN DEADLINE"
  29. }
  30. }

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

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

英文:

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

  1. {
  2. "id": 1513,
  3. "procedure": {
  4. "title": "Sample Procedure 1"
  5. },
  6. "category": {
  7. "title": "Press"
  8. },
  9. "status": {
  10. "status": "IN PROCESS"
  11. },
  12. "deadline": {
  13. "deadline": "OUT OF DEADLINE"
  14. }
  15. },
  16. {
  17. "id": 1514,
  18. "procedure": {
  19. "title": "Sample Procedure 2"
  20. },
  21. "category": {
  22. "title": "Press"
  23. },
  24. "status": {
  25. "status": "SUBMITTED"
  26. },
  27. "deadline": {
  28. "deadline": "WITHIN DEADLINE"
  29. }
  30. },

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

  1. const arr = [{
  2. "id": 1513,
  3. "procedure": {
  4. "title": "Sample Procedure 1"
  5. },
  6. "category": {
  7. "title": "Press"
  8. },
  9. "status": {
  10. "status": "IN PROCESS"
  11. },
  12. "deadline": {
  13. "deadline": "OUT OF DEADLINE"
  14. }
  15. },
  16. {
  17. "id": 1514,
  18. "procedure": {
  19. "title": "Sample Procedure 2"
  20. },
  21. "category": {
  22. "title": "Press"
  23. },
  24. "status": {
  25. "status": "SUBMITTED"
  26. },
  27. "deadline": {
  28. "deadline": "WITHIN DEADLINE"
  29. }
  30. },
  31. {
  32. "id": 1515,
  33. "procedure": {
  34. "title": "Sample Procedure 3"
  35. },
  36. "category": {
  37. "title": "Press"
  38. },
  39. "status": {
  40. "status": "IN PROCESS"
  41. },
  42. "deadline": {
  43. "deadline": "WITHIN DEADLINE"
  44. }
  45. }]
  46. const result = arr.filter(item => item.status.status === 'IN PROCESS'
  47. && item.deadline.deadline === 'WITHIN DEADLINE')
  48. console.log(result)

Output:

  1. [
  2. {
  3. id: 1515,
  4. procedure: { title: 'Sample Procedure 3' },
  5. category: { title: 'Press' },
  6. status: { status: 'IN PROCESS' },
  7. deadline: { deadline: 'WITHIN DEADLINE' }
  8. }
  9. ]

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

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

Reference link

英文:
  1. const arr = [{
  2. "id": 1513,
  3. "procedure": {
  4. "title": "Sample Procedure 1"
  5. },
  6. "category": {
  7. "title": "Press"
  8. },
  9. "status": {
  10. "status": "IN PROCESS"
  11. },
  12. "deadline": {
  13. "deadline": "OUT OF DEADLINE"
  14. }
  15. },
  16. {
  17. "id": 1514,
  18. "procedure": {
  19. "title": "Sample Procedure 2"
  20. },
  21. "category": {
  22. "title": "Press"
  23. },
  24. "status": {
  25. "status": "SUBMITTED"
  26. },
  27. "deadline": {
  28. "deadline": "WITHIN DEADLINE"
  29. }
  30. },
  31. {
  32. "id": 1515,
  33. "procedure": {
  34. "title": "Sample Procedure 3"
  35. },
  36. "category": {
  37. "title": "Press"
  38. },
  39. "status": {
  40. "status": "IN PROCESS"
  41. },
  42. "deadline": {
  43. "deadline": "WITHIN DEADLINE"
  44. }
  45. },]
  46. const result = arr.filter(item => item.status.status === 'IN PROCESS'
  47. && item.deadline.deadline === 'WITHIN DEADLINE')
  48. console.log(result)

Output:

  1. [
  2. {
  3. id: 1515,
  4. procedure: { title: 'Sample Procedure 3' },
  5. category: { title: 'Press' },
  6. status: { status: 'IN PROCESS' },
  7. deadline: { deadline: 'WITHIN DEADLINE' }
  8. }
  9. ]

Edit:

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

  1. createQueryBuilder("entity")
  2. .where("entity.status = :status", { status: "IN PROCESS" })
  3. .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:

确定