如何根据多个条件筛选对象中的数组

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

How to filter array in object by few conditions

问题

以下是代码的翻译部分:

  1. 我有这样一个具有newComments属性的对象
  2. const newComments = {
  3. commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  4. comments: [
  5. {
  6. id: "123",
  7. status: {
  8. entity: null,
  9. id: "1a913152-7809-ec11-8daa-90600b960f93",
  10. name: "In work",
  11. parentId: null,
  12. },
  13. },
  14. {
  15. id: "124",
  16. status: {
  17. entity: null,
  18. id: "1a913152-7809-ec11-8daa-90600b960f94",
  19. name: "Note",
  20. parentId: null,
  21. },
  22. },
  23. {
  24. id: "125",
  25. status: {
  26. entity: null,
  27. id: "1a913152-7809-ec11-8daa-90600b960f95",
  28. name: "Canceled",
  29. parentId: null,
  30. },
  31. },
  32. {
  33. id: "126",
  34. status: {
  35. entity: null,
  36. id: "1a913152-7809-ec11-8daa-90600b960f96",
  37. name: "Done",
  38. parentId: null,
  39. },
  40. },
  41. ],
  42. dataType: "Tags",
  43. idAttributeApprovalName: "12-015-123",
  44. };

还有筛选条件:

  1. const values = ["Note", "Canceled", "Done"];

我的任务是返回与这些键不相等的评论:

  1. comment.status.name !== "Note" | "Canceled" | "Done"

换句话说:

  1. const newComments = {
  2. commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  3. comments: [
  4. {
  5. id: "123",
  6. status: {
  7. entity: null,
  8. id: "1a913152-7809-ec11-8daa-90600b960f93",
  9. name: "In work",
  10. parentId: null,
  11. },
  12. },
  13. ],
  14. dataType: "Tags",
  15. idAttributeApprovalName: "12-015-123",
  16. };
英文:

I have such an object with newComments:

  1. const newComments = {
  2. commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  3. comments: [
  4. {
  5. id: "123",
  6. status: {
  7. entity: null,
  8. id: "1a913152-7809-ec11-8daa-90600b960f93",
  9. name: "In work",
  10. parentId: null,
  11. },
  12. },
  13. {
  14. id: "124",
  15. status: {
  16. entity: null,
  17. id: "1a913152-7809-ec11-8daa-90600b960f94",
  18. name: "Note",
  19. parentId: null,
  20. },
  21. },
  22. {
  23. id: "125",
  24. status: {
  25. entity: null,
  26. id: "1a913152-7809-ec11-8daa-90600b960f95",
  27. name: "Canceled",
  28. parentId: null,
  29. },
  30. },
  31. {
  32. id: "126",
  33. status: {
  34. entity: null,
  35. id: "1a913152-7809-ec11-8daa-90600b960f96",
  36. name: "Done",
  37. parentId: null,
  38. },
  39. },
  40. ],
  41. dataType: "Tags",
  42. idAttributeApprovalName: "12-015-123",
  43. };

There are also filters:

  1. const values = ["Note", "Canceled", "Done"];

My task is to return only comments that are not equal to keys:

  1. comment.status.name !== "Note" | "Canceled" |"Done"

In other words:

  1. const newComments = {
  2. commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  3. comments: [
  4. {
  5. id: "123",
  6. status: {
  7. entity: null,
  8. id: "1a913152-7809-ec11-8daa-90600b960f93",
  9. name: "In work",
  10. parentId: null,
  11. },
  12. },
  13. ],
  14. dataType: "Tags",
  15. idAttributeApprovalName: "12-015-123",
  16. };

答案1

得分: 0

你可以使用 Array#filterArray#everyArray#includes 结合使用。

  1. const newComments = {
  2. commentDirectoryId: "ee63997c-01d5-ec11-8dad-e116bd673e14",
  3. comments: [
  4. { id: "123", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f93", name: "In work", parentId: null } },
  5. { id: "124", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f94", name: "Note", parentId: null } },
  6. { id: "125", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f95", name: "Canceled", parentId: null } },
  7. { id: "126", status: { entity: null, id: "1a913152-7809-ec11-8daa-90600b960f96", name: "Done", parentId: null } },
  8. ],
  9. dataType: "Tags",
  10. idAttributeApprovalName: "12-015-123"
  11. };
  12. const values = ["Note", "Canceled", "Done"];
  13. newComments.comments = newComments.comments.filter(c =>
  14. values.every(v => c.status.name !== v));
  15. // 或者使用 !values.includes(c.status.name) 如果不需要复杂的匹配逻辑
  16. console.log(newComments);

请注意,以上代码片段是用JavaScript编写的,只提供代码部分的翻译。

英文:

You can use Array#filter in conjunction with Array#every or Array#includes.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const newComments={commentDirectoryId:&quot;ee63997c-01d5-ec11-8dad-e116bd673e14&quot;,comments:[{id:&quot;123&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f93&quot;,name:&quot;In work&quot;,parentId:null}},{id:&quot;124&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f94&quot;,name:&quot;Note&quot;,parentId:null}},{id:&quot;125&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f95&quot;,name:&quot;Canceled&quot;,parentId:null}},{id:&quot;126&quot;,status:{entity:null,id:&quot;1a913152-7809-ec11-8daa-90600b960f96&quot;,name:&quot;Done&quot;,parentId:null}},],dataType:&quot;Tags&quot;,idAttributeApprovalName:&quot;12-015-123&quot;};
  2. const values = [&quot;Note&quot;, &quot;Canceled&quot;, &quot;Done&quot;];
  3. newComments.comments = newComments.comments.filter(c =&gt;
  4. values.every(v =&gt; c.status.name !== v));
  5. // or !values.includes(c.status.name) if no complex matching logic is required
  6. console.log(newComments);

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年2月7日 01:41:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364764.html
匿名

发表评论

匿名网友

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

确定