在JavaScript中,获取数组对象中与键对应的唯一值

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

Get unique values corresponding to the keys from an array of objects in JavaScript

问题

我的数组如下所示:

  1. const arr1 = [
  2. {
  3. "Param1": "20",
  4. "Param2": "8",
  5. "Param3": "11",
  6. "Param4": "4",
  7. "Param5": "18",
  8. "Param6": "20",
  9. "Param7": "8"
  10. },
  11. {
  12. "Param6": "21",
  13. "Param7": "8",
  14. "Param8": "11",
  15. "Param9": "4",
  16. "Param10": "18"
  17. },
  18. {
  19. "Param1": "20",
  20. "Param2": "8",
  21. "Param3": "10"
  22. }
  23. ]

我想要删除对象中存在的重复键值对。
期望结果:

  1. arr1 = [
  2. {
  3. "Param1": "20",
  4. "Param2": "8",
  5. "Param3": "11",
  6. "Param4": "4",
  7. "Param5": "18",
  8. "Param6": "20",
  9. "Param7": "8"
  10. },
  11. {
  12. "Param6": "21",
  13. "Param8": "11",
  14. "Param9": "4",
  15. "Param10": "18"
  16. },
  17. {
  18. "Param3": "10"
  19. }
  20. ]

期望的输出是一个包含具有唯一键值对的对象的数组。如何获得这样的数组?

英文:

My array looks like this

  1. const arr1 = [
  2. {
  3. "Param1": "20",
  4. "Param2": "8",
  5. "Param3": "11",
  6. "Param4": "4",
  7. "Param5": "18",
  8. "Param6": "20",
  9. "Param7": "8"
  10. },
  11. {
  12. "Param6": "21",
  13. "Param7": "8",
  14. "Param8": "11",
  15. "Param9": "4",
  16. "Param10": "18"
  17. },
  18. {
  19. "Param1": "20",
  20. "Param2": "8",
  21. "Param3": "10"
  22. }
  23. ]

I wanted to remove the duplicate key value pairs existing in the objects.
Expected result:

  1. arr1 = [
  2. {
  3. "Param1": "20",
  4. "Param2": "8",
  5. "Param3": "11",
  6. "Param4": "4",
  7. "Param5": "18",
  8. "Param6": "20",
  9. "Param7": "8"
  10. },
  11. {
  12. "Param6": "21",
  13. "Param8": "11",
  14. "Param9": "4",
  15. "Param10": "18"
  16. },
  17. {
  18. "Param3": "10"
  19. }
  20. ]

The desired output is an array containing objects having unique key value pairs. How can I get such an array?

答案1

得分: 2

Algorithm:

  • 创建一个以{param: {value: boolean}}格式的seen映射,用于指示是否已经看到具有这个值的参数。
  • 创建一个result数组,用于存储唯一的参数。
  • 遍历输入数组,获取当前元素的键,并遍历这些键。
  • 检查具有这个值的参数是否已经被看到,如果是,则忽略它;否则将其添加到result中并标记为已看到。
  • 返回result数组。

Implementation:

  1. const removeDuplicates = (arr) => {
  2. return arr.reduce(
  3. (acc, item) => {
  4. acc.result.push(
  5. Object.fromEntries(
  6. Object.entries(item).filter(([key, value]) => {
  7. acc.seen[key] = acc.seen[key] ?? {};
  8. if (acc.seen[key][value]) return false;
  9. acc.seen[key][value] = true;
  10. return true;
  11. }),
  12. ),
  13. );
  14. return acc;
  15. },
  16. { seen: {}, result: [] },
  17. ).result;
  18. };

playground

英文:

Algorithm:

  • Create a map seen in the format of {param: {value: boolean}} which will indicate whether the param with this value was already seen
  • Create a result array that will store the unique params
  • Traverse through the input array, get the keys of the current element, and traverse through them.
  • Check if the param with this value was already seen, if yes, ignore it; otherwise add it to the result and mark it as seen
  • Return the result array.

Implementation:

  1. const removeDuplicates = (arr: Record<string, string>[]) => {
  2. return arr.reduce<{
  3. seen: Record<string, Record<string, boolean>>;
  4. result: Record<string, string>[];
  5. }>(
  6. (acc, item) => {
  7. acc.result.push(
  8. Object.fromEntries(
  9. Object.entries(item).filter(([key, value]) => {
  10. acc.seen[key] = acc.seen[key] ?? {};
  11. if (acc.seen[key][value]) return false;
  12. acc.seen[key][value] = true;
  13. return true;
  14. }),
  15. ),
  16. );
  17. return acc;
  18. },
  19. { seen: {}, result: [] },
  20. ).result;
  21. };

playground

huangapple
  • 本文由 发表于 2023年6月22日 17:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76530384.html
匿名

发表评论

匿名网友

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

确定