在Angular中比较两个不同的对象并移除重复项。

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

In Angular compare two different objects and remove duplicates

问题

在Angular中,我有这两个包含对象的数组。

  1. 主要对象
  1. [
  2. {
  3. "userId": 8,
  4. "name": "Adfgh",
  5. "uniqueId": "rthj",
  6. "source": {
  7. "key": "0",
  8. "mode": "d",
  9. "name": "tev5"
  10. },
  11. "headers": [
  12. {
  13. "content": "b",
  14. "id": "01"
  15. },
  16. {
  17. "content": "m",
  18. "id": "04"
  19. },
  20. {
  21. "content": "R1",
  22. "id": "02"
  23. },
  24. {
  25. "content": "083083",
  26. "id": "03"
  27. },
  28. {
  29. "content": "5",
  30. "id": "05"
  31. }
  32. ],
  33. "key": "IPP Master Key",
  34. "destination": "5"
  35. },
  36. {
  37. "userId": 8,
  38. "name": "FGHJKNJKN",
  39. "uniqueId": "etjhjub",
  40. "source": {
  41. "key": "M0",
  42. "mode": "C",
  43. "name": "DEVm6"
  44. },
  45. "headers": [
  46. {
  47. "content": "n",
  48. "id": "01"
  49. },
  50. {
  51. "content": "m",
  52. "id": "04"
  53. },
  54. {
  55. "content": "R1",
  56. "id": "02"
  57. },
  58. {
  59. "content": "083083",
  60. "id": "03"
  61. },
  62. {
  63. "content": "6",
  64. "id": "05"
  65. }
  66. ],
  67. "key": "IPP Master Key",
  68. "destination": "6"
  69. },
  70. {
  71. "userId": 8,
  72. "name": "A-KEYTST-DEVm7",
  73. "VRKcustomerId": "VHQ-DUKPT-MSK",
  74. "sourceKeyObject": {
  75. "keyUsage": "M0",
  76. "modeOfUse": "C",
  77. "name": "DEVm7"
  78. },
  79. "headers": [
  80. {
  81. "content": "n",
  82. "id": "01"
  83. },
  84. {
  85. "content": "m",
  86. "id": "04"
  87. },
  88. {
  89. "content": "R1",
  90. "id": "02"
  91. },
  92. {
  93. "content": "083083",
  94. "id": "03"
  95. },
  96. {
  97. "content": "7",
  98. "id": "05"
  99. }
  100. ],
  101. "keyType": "IPP Master Key",
  102. "destination": "7"
  103. },
  104. {
  105. "customerId": 8,
  106. "name": "name",
  107. "uniqueId": "remove",
  108. "source": {
  109. "key": "D0",
  110. "mode": "B",
  111. "name": "A-KEYTST-PNP-NHMAC"
  112. },
  113. "headers": [
  114. {
  115. "content": "1",
  116. "id": "11"
  117. },
  118. {
  119. "content": "ESDFGH_567",
  120. "id": "10"
  121. }
  122. ],
  123. "key": null,
  124. "destination": ""
  125. },
  126. {},
  127. {},
  128. {},
  129. {},
  130. {},
  131. {},
  132. {},
  133. ]
  1. 第二个对象
  1. [
  2. {
  3. "Id": 098,
  4. "UniqueId": "remove",
  5. "Destination": "",
  6. "Key": "",
  7. "Name": "name",
  8. "Enable": false,
  9. "Auto": 0
  10. },
  11. {
  12. "Id": 154,
  13. "UniqueId": "NEO-TEST",
  14. "Destination": "",
  15. "Key": "",
  16. "Name": "A-KEYTST-PNP-NHMAC",
  17. "Enable": false,
  18. "Auto": 0
  19. },
  20. ]

期望结果。

  1. [
  2. {
  3. "userId": 8,
  4. "name": "Adfgh",
  5. "uniqueId": "rthj",
  6. "source": {
  7. "key": "0",
  8. "mode": "d",
  9. "name": "tev5"
  10. },
  11. "headers": [
  12. {
  13. "content": "b",
  14. "id": "01"
  15. },
  16. {
  17. "content": "m",
  18. "id": "04"
  19. },
  20. {
  21. "content": "R1",
  22. "id": "02"
  23. },
  24. {
  25. "content": "083083",
  26. "id": "03"
  27. },
  28. {
  29. "content": "5",
  30. "id": "05"
  31. }
  32. ],
  33. "key": "IPP Master Key",
  34. "destination": "5"
  35. },
  36. {
  37. "userId": 8,
  38. "name": "FGHJKNJKN",
  39. "uniqueId": "etjhjub",
  40. "source": {
  41. "key": "M0",
  42. "mode": "C",
  43. "name": "DEVm6"
  44. },
  45. "headers": [
  46. {
  47. "content": "n",
  48. "id": "01"
  49. },
  50. {
  51. "content": "m",
  52. "id": "04"
  53. },
  54. {
  55. "content": "R1",
  56. "id": "02"
  57. },
  58. {
  59. "content": "083083",
  60. "id": "03"
  61. },
  62. {
  63. "content": "6",
  64. "id": "05"
  65. }
  66. ],
  67. "key": "IPP Master Key",
  68. "destination": "6"
  69. },
  70. {
  71. "userId": 8,
  72. "name": "A-KEYTST-DEVm7",
  73. "VRKcustomerId": "VHQ-DUKPT-MSK",
  74. "sourceKeyObject": {
  75. "keyUsage": "M0",
  76. "modeOfUse": "C",
  77. "name": "DEVm7"
  78. },
  79. "headers": [
  80. {
  81. "content": "n",
  82. "id": "01"
  83. },
  84. {
  85. "content": "m",
  86. "id": "04"
  87. },
  88. {
  89. "content": "R1",
  90. "id": "02"
  91. },
  92. {
  93. "content": "083083",
  94. "id": "03"
  95. },
  96. {
  97. "content": "7",
  98. "id": "05"
  99. }
  100. ],
  101. "keyType": "IPP Master Key",
  102. "destination": "7"
  103. },
  104. ]

要比较它们,我创建了一个分离的数组,以使它们相似。我尝试使用过滤器和映射,但没有帮助。

我尝试了不同的方法,但没有成功。所以我没有代码片段。需要根据它们之间的差异比较主要对象和第二个对象,然后修改主要对象。

英文:

In Angular I have this 2 arrays with objects.

  1. Main Object

    [
    {
    "userId": 8,
    "name": "Adfgh",
    "uniqueId": "rthj",
    "source": {
    "key": "0",
    "mode": "d",
    "name": "tev5"
    },
    "headers": [
    {
    "content": "b",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "5",
    "id": "05"
    }
    ],
    "key": "IPP Master Key",
    "destination": "5"
    },
    {
    "userId": 8,
    "name": "FGHJKNJKN",
    "uniqueId": "etjhjub",
    "source": {
    "key": "M0",
    "mode": "C",
    "name": "DEVm6"
    },
    "headers": [
    {
    "content": "n",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "6",
    "id": "05"
    }
    ],
    "key": "IPP Master Key",
    "destination": "6"
    },
    {
    "userId": 8,
    "name": "A-KEYTST-DEVm7",
    "VRKcustomerId": "VHQ-DUKPT-MSK",
    "sourceKeyObject": {
    "keyUsage": "M0",
    "modeOfUse": "C",
    "name": "DEVm7"
    },
    "headers": [
    {
    "content": "n",
    "id": "01"
    },
    {
    "content": "m",
    "id": "04"
    },
    {
    "content": "R1",
    "id": "02"
    },
    {
    "content": "083083",
    "id": "03"
    },
    {
    "content": "7",
    "id": "05"
    }
    ],
    "keyType": "IPP Master Key",
    "destination": "7"
    },
    {
    "customerId": 8,
    "name": "name",
    "uniqueId": "remove",
    "source": {
    "key": "D0",
    "mode": "B",
    "name": "A-KEYTST-PNP-NHMAC"
    },
    "headers": [
    {
    "content": "1",
    "id": "11"
    },
    {
    "content": "ESDFGH_567",
    "id": "10"
    }
    ],
    "key": null,
    "destination": ""
    },
    {},
    {},
    {},
    {},
    {},
    {},
    {},
    ]

  2. second object

    [
    {
    "Id": 098,
    "UniqueId": "remove",
    "Destination": "",
    "Key": "",
    "Name": "name",
    "Enable": false,
    "Auto": 0
    },
    {
    "Id": 154,
    "UniqueId": "NEO-TEST",
    "Destination": "",
    "Key": "",
    "Name": "A-KEYTST-PNP-NHMAC",
    "Enable": false,
    "Auto": 0
    },
    ]

expected result.

  1. [
  2. {
  3. "userId": 8,
  4. "name": "Adfgh",
  5. "uniqueId": "rthj",
  6. "source": {
  7. "key": "0",
  8. "mode": "d",
  9. "name": "tev5"
  10. },
  11. "headers": [
  12. {
  13. "content": "b",
  14. "id": "01"
  15. },
  16. {
  17. "content": "m",
  18. "id": "04"
  19. },
  20. {
  21. "content": "R1",
  22. "id": "02"
  23. },
  24. {
  25. "content": "083083",
  26. "id": "03"
  27. },
  28. {
  29. "content": "5",
  30. "id": "05"
  31. }
  32. ],
  33. "key": "IPP Master Key",
  34. "destination": "5"
  35. },
  36. {
  37. "userId": 8,
  38. "name": "FGHJKNJKN",
  39. "uniqueId": "etjhjub",
  40. "source": {
  41. "key": "M0",
  42. "mode": "C",
  43. "name": "DEVm6"
  44. },
  45. "headers": [
  46. {
  47. "content": "n",
  48. "id": "01"
  49. },
  50. {
  51. "content": "m",
  52. "id": "04"
  53. },
  54. {
  55. "content": "R1",
  56. "id": "02"
  57. },
  58. {
  59. "content": "083083",
  60. "id": "03"
  61. },
  62. {
  63. "content": "6",
  64. "id": "05"
  65. }
  66. ],
  67. "key": "IPP Master Key",
  68. "destination": "6"
  69. },
  70. {
  71. "userId": 8,
  72. "name": "A-KEYTST-DEVm7",
  73. "VRKcustomerId": "VHQ-DUKPT-MSK",
  74. "sourceKeyObject": {
  75. "keyUsage": "M0",
  76. "modeOfUse": "C",
  77. "name": "DEVm7"
  78. },
  79. "headers": [
  80. {
  81. "content": "n",
  82. "id": "01"
  83. },
  84. {
  85. "content": "m",
  86. "id": "04"
  87. },
  88. {
  89. "content": "R1",
  90. "id": "02"
  91. },
  92. {
  93. "content": "083083",
  94. "id": "03"
  95. },
  96. {
  97. "content": "7",
  98. "id": "05"
  99. }
  100. ],
  101. "keyType": "IPP Master Key",
  102. "destination": "7"
  103. },
  104. ]

For to compare them, I created a seperated array for to make them similiar to each other. Used filter, map but it wasn't helpful.

I have tried in different ways, and nothing worked. So I don't have the snippet for the code.
Need to compare main object and second object, based on the difference between them have to modify main object.

答案1

得分: 0

Sure, here's the translated code:

  1. function updateMainObject(mainArray: any[], secondArray: any[]) {
  2. const updatedMainArray = mainArray.filter((mainItem) => {
  3. return (
  4. mainItem &&
  5. ((mainItem.uniqueId && !secondArray.some((secondItem) => mainItem.uniqueId === secondItem.UniqueId)) ||
  6. (mainItem.userId && !secondArray.some((secondItem) => mainItem.userId === secondItem.Id)))
  7. );
  8. });
  9. return updatedMainArray;
  10. }

You can play with it here.

英文:

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

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

  1. function updateMainObject(mainArray: any[], secondArray: any[]) {
  2. const updatedMainArray = mainArray.filter((mainItem) =&gt; {
  3. return (
  4. mainItem &amp;&amp;
  5. ((mainItem.uniqueId &amp;&amp; !secondArray.some((secondItem) =&gt; mainItem.uniqueId === secondItem.UniqueId)) ||
  6. (mainItem.userId &amp;&amp; !secondArray.some((secondItem) =&gt; mainItem.userId === secondItem.Id)))
  7. );
  8. });
  9. return updatedMainArray;
  10. }

<!-- end snippet -->

You can play with it there

huangapple
  • 本文由 发表于 2023年8月4日 22:55:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837054.html
匿名

发表评论

匿名网友

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

确定