你可以有效地从JavaScript对象中检索“subjects”属性的所有可能值吗?

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

How can I efficiently retrieve all possible values of the 'subjects' property from an object in JavaScript?

问题

我有一个具有以下结构的对象:

  1. [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "化学"
  8. },
  9. {
  10. "id": 13,
  11. "name": "物理"
  12. },
  13. {
  14. "id": 14,
  15. "name": "心理学"
  16. },
  17. {
  18. "id": 16,
  19. "name": "历史"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "数学"
  29. },
  30. {
  31. "id": 12,
  32. "name": "化学"
  33. },
  34. {
  35. "id": 14,
  36. "name": "生物学"
  37. },
  38. {
  39. "id": 15,
  40. "name": "地理"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "生物学"
  50. },
  51. {
  52. "id": 15,
  53. "name": "地理"
  54. },
  55. {
  56. "id": 16,
  57. "name": "历史"
  58. }
  59. ]
  60. }
  61. ]

在这个选项中,获取subjects属性的所有可能值的最快方式是循环遍历每个子对象,并将其值推送到“计数数组”中,如果它尚未存在。

英文:

I had an object with this structure:

  1. [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "Chemistry"
  8. },
  9. {
  10. "id": 13,
  11. "name": "Physics"
  12. },
  13. {
  14. "id": 14,
  15. "name": "Psychology"
  16. },
  17. {
  18. "id": 16,
  19. "name": "History"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "Maths"
  29. },
  30. {
  31. "id": 12,
  32. "name": "Chemistry"
  33. },
  34. {
  35. "id": 14,
  36. "name": "Biology"
  37. },
  38. {
  39. "id": 15,
  40. "name": "Geography"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "Biology"
  50. },
  51. {
  52. "id": 15,
  53. "name": "Geography"
  54. },
  55. {
  56. "id": 16,
  57. "name": "History"
  58. }
  59. ]
  60. }
  61. ]

What will be the fastest way to get all possible values of subjects prop in this option? Is this only possible to achieve by looping through each single sub-object and push value to "counting array" if it not already there?

答案1

得分: 1

The simplest and most readable way would indeed be to loop through the subjects and add them to the array if they don't exist.

  1. const data = [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "Chemistry"
  8. },
  9. {
  10. "id": 13,
  11. "name": "Physics"
  12. },
  13. {
  14. "id": 14,
  15. "name": "Psychology"
  16. },
  17. {
  18. "id": 16,
  19. "name": "History"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "Maths"
  29. },
  30. {
  31. "id": 12,
  32. "name": "Chemistry"
  33. },
  34. {
  35. "id": 14,
  36. "name": "Biology"
  37. },
  38. {
  39. "id": 15,
  40. "name": "Geography"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "Biology"
  50. },
  51. {
  52. "id": 15,
  53. "name": "Geography"
  54. },
  55. {
  56. "id": 16,
  57. "name": "History"
  58. }
  59. ]
  60. }
  61. ]
  62. const all = []
  63. for (const { subjects } of data) {
  64. subjects.forEach((s) => {
  65. if (all.indexOf(s.name) === -1) {
  66. all.push(s.name)
  67. }
  68. })
  69. }
  70. console.log(all)

Another useful way is to use a Set with spread syntax to deduplicate the values in the array.

  1. const data = [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "Chemistry"
  8. },
  9. {
  10. "id": 13,
  11. "name": "Physics"
  12. },
  13. {
  14. "id": 14,
  15. "name": "Psychology"
  16. },
  17. {
  18. "id": 16,
  19. "name": "History"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "Maths"
  29. },
  30. {
  31. "id": 12,
  32. "name": "Chemistry"
  33. },
  34. {
  35. "id": 14,
  36. "name": "Biology"
  37. },
  38. {
  39. "id": 15,
  40. "name": "Geography"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "Biology"
  50. },
  51. {
  52. "id": 15,
  53. "name": "Geography"
  54. },
  55. {
  56. "id": 16,
  57. "name": "History"
  58. }
  59. ]
  60. }
  61. ]
  62. const all = []
  63. for (const { subjects } of data) {
  64. subjects.forEach((s) => {
  65. all.push(s.name)
  66. })
  67. }
  68. const s = [...new Set(all)]
  69. console.log(s)
英文:

The simplest and most readable way would indeed be to loop through the subjects and add them to the array if they don't exist.

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

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

  1. const data = [
  2. {
  3. &quot;event_id&quot;: 1,
  4. &quot;subjects&quot;: [
  5. {
  6. &quot;id&quot;: 12,
  7. &quot;name&quot;: &quot;Chemistry&quot;
  8. },
  9. {
  10. &quot;id&quot;: 13,
  11. &quot;name&quot;: &quot;Physics&quot;
  12. },
  13. {
  14. &quot;id&quot;: 14,
  15. &quot;name&quot;: &quot;Psychology&quot;
  16. },
  17. {
  18. &quot;id&quot;: 16,
  19. &quot;name&quot;: &quot;History&quot;
  20. }
  21. ]
  22. },
  23. {
  24. &quot;event_id&quot;: 2,
  25. &quot;subjects&quot;: [
  26. {
  27. &quot;id&quot;: 11,
  28. &quot;name&quot;: &quot;Maths&quot;
  29. },
  30. {
  31. &quot;id&quot;: 12,
  32. &quot;name&quot;: &quot;Chemistry&quot;
  33. },
  34. {
  35. &quot;id&quot;: 14,
  36. &quot;name&quot;: &quot;Biology&quot;
  37. },
  38. {
  39. &quot;id&quot;: 15,
  40. &quot;name&quot;: &quot;Geography&quot;
  41. }
  42. ]
  43. },
  44. {
  45. &quot;event_id&quot;: 3,
  46. &quot;subjects&quot;: [
  47. {
  48. &quot;id&quot;: 14,
  49. &quot;name&quot;: &quot;Biology&quot;
  50. },
  51. {
  52. &quot;id&quot;: 15,
  53. &quot;name&quot;: &quot;Geography&quot;
  54. },
  55. {
  56. &quot;id&quot;: 16,
  57. &quot;name&quot;: &quot;History&quot;
  58. }
  59. ]
  60. }
  61. ]
  62. const all = []
  63. for (const { subjects } of data) {
  64. subjects.forEach((s) =&gt; {
  65. if (all.indexOf(s.name) === -1) {
  66. all.push(s.name)
  67. }
  68. })
  69. }
  70. console.log(all)

<!-- end snippet -->

Another useful way is to use a Set with spread syntax to deduplicate the values in the array.

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

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

  1. const data = [
  2. {
  3. &quot;event_id&quot;: 1,
  4. &quot;subjects&quot;: [
  5. {
  6. &quot;id&quot;: 12,
  7. &quot;name&quot;: &quot;Chemistry&quot;
  8. },
  9. {
  10. &quot;id&quot;: 13,
  11. &quot;name&quot;: &quot;Physics&quot;
  12. },
  13. {
  14. &quot;id&quot;: 14,
  15. &quot;name&quot;: &quot;Psychology&quot;
  16. },
  17. {
  18. &quot;id&quot;: 16,
  19. &quot;name&quot;: &quot;History&quot;
  20. }
  21. ]
  22. },
  23. {
  24. &quot;event_id&quot;: 2,
  25. &quot;subjects&quot;: [
  26. {
  27. &quot;id&quot;: 11,
  28. &quot;name&quot;: &quot;Maths&quot;
  29. },
  30. {
  31. &quot;id&quot;: 12,
  32. &quot;name&quot;: &quot;Chemistry&quot;
  33. },
  34. {
  35. &quot;id&quot;: 14,
  36. &quot;name&quot;: &quot;Biology&quot;
  37. },
  38. {
  39. &quot;id&quot;: 15,
  40. &quot;name&quot;: &quot;Geography&quot;
  41. }
  42. ]
  43. },
  44. {
  45. &quot;event_id&quot;: 3,
  46. &quot;subjects&quot;: [
  47. {
  48. &quot;id&quot;: 14,
  49. &quot;name&quot;: &quot;Biology&quot;
  50. },
  51. {
  52. &quot;id&quot;: 15,
  53. &quot;name&quot;: &quot;Geography&quot;
  54. },
  55. {
  56. &quot;id&quot;: 16,
  57. &quot;name&quot;: &quot;History&quot;
  58. }
  59. ]
  60. }
  61. ]
  62. const all = []
  63. for (const { subjects } of data) {
  64. subjects.forEach((s) =&gt; {
  65. all.push(s.name)
  66. })
  67. }
  68. const s = [...new Set(all)]
  69. console.log(s)

<!-- end snippet -->

答案2

得分: 0

以下是代码的翻译部分:

  1. data.reduce((accumulator, currentValue) => {
  2. accumulator.push(currentValue.subjects.map(x => x.name));
  3. return [...new Set(accumulator.flatMap(x => x))];
  4. }, [])
  5. // 输出结果
  6. // ['化学', '物理', '心理学', '历史', '数学', '生物学', '地理学']
  1. const data = [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "化学"
  8. },
  9. {
  10. "id": 13,
  11. "name": "物理"
  12. },
  13. {
  14. "id": 14,
  15. "name": "心理学"
  16. },
  17. {
  18. "id": 16,
  19. "name": "历史"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "数学"
  29. },
  30. {
  31. "id": 12,
  32. "name": "化学"
  33. },
  34. {
  35. "id": 14,
  36. "name": "生物学"
  37. },
  38. {
  39. "id": 15,
  40. "name": "地理学"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "生物学"
  50. },
  51. {
  52. "id": 15,
  53. "name": "地理学"
  54. },
  55. {
  56. "id": 16,
  57. "name": "历史"
  58. }
  59. ]
  60. }
  61. ];
  62. console.log(data.reduce((accumulator, currentValue) => {
  63. accumulator.push(currentValue.subjects.map(x => x.name));
  64. return [...new Set(accumulator.flatMap(x => x))];
  65. }, []));

希望这能帮助您理解代码和结果。

英文:

just because there's several ways to do the same things, here's one with reduce

  1. data.reduce((accumulator, currentValue) =&gt; {
  2. accumulator.push(currentValue.subjects.map(x =&gt; x.name));
  3. return [...new Set(accumulator.flatMap(x =&gt; x))];
  4. }, [])
  5. // will output
  6. // [&#39;Chemistry&#39;, &#39;Physics&#39;, &#39;Psychology&#39;, &#39;History&#39;, &#39;Maths&#39;, &#39;Biology&#39;, &#39;Geography&#39;]

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

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

  1. const data = [
  2. {
  3. &quot;event_id&quot;: 1,
  4. &quot;subjects&quot;: [
  5. {
  6. &quot;id&quot;: 12,
  7. &quot;name&quot;: &quot;Chemistry&quot;
  8. },
  9. {
  10. &quot;id&quot;: 13,
  11. &quot;name&quot;: &quot;Physics&quot;
  12. },
  13. {
  14. &quot;id&quot;: 14,
  15. &quot;name&quot;: &quot;Psychology&quot;
  16. },
  17. {
  18. &quot;id&quot;: 16,
  19. &quot;name&quot;: &quot;History&quot;
  20. }
  21. ]
  22. },
  23. {
  24. &quot;event_id&quot;: 2,
  25. &quot;subjects&quot;: [
  26. {
  27. &quot;id&quot;: 11,
  28. &quot;name&quot;: &quot;Maths&quot;
  29. },
  30. {
  31. &quot;id&quot;: 12,
  32. &quot;name&quot;: &quot;Chemistry&quot;
  33. },
  34. {
  35. &quot;id&quot;: 14,
  36. &quot;name&quot;: &quot;Biology&quot;
  37. },
  38. {
  39. &quot;id&quot;: 15,
  40. &quot;name&quot;: &quot;Geography&quot;
  41. }
  42. ]
  43. },
  44. {
  45. &quot;event_id&quot;: 3,
  46. &quot;subjects&quot;: [
  47. {
  48. &quot;id&quot;: 14,
  49. &quot;name&quot;: &quot;Biology&quot;
  50. },
  51. {
  52. &quot;id&quot;: 15,
  53. &quot;name&quot;: &quot;Geography&quot;
  54. },
  55. {
  56. &quot;id&quot;: 16,
  57. &quot;name&quot;: &quot;History&quot;
  58. }
  59. ]
  60. }
  61. ];
  62. console.log(data.reduce((accumulator, currentValue) =&gt; {
  63. accumulator.push(currentValue.subjects.map(x=&gt;x.name));
  64. return [...new Set(accumulator.flatMap(x=&gt;x))];
  65. }, []));

<!-- end snippet -->

答案3

得分: 0

  1. const data = [
  2. {
  3. "event_id": 1,
  4. "subjects": [
  5. {
  6. "id": 12,
  7. "name": "化学"
  8. },
  9. {
  10. "id": 13,
  11. "name": "物理"
  12. },
  13. {
  14. "id": 14,
  15. "name": "心理学"
  16. },
  17. {
  18. "id": 16,
  19. "name": "历史"
  20. }
  21. ]
  22. },
  23. {
  24. "event_id": 2,
  25. "subjects": [
  26. {
  27. "id": 11,
  28. "name": "数学"
  29. },
  30. {
  31. "id": 12,
  32. "name": "化学"
  33. },
  34. {
  35. "id": 14,
  36. "name": "生物学"
  37. },
  38. {
  39. "id": 15,
  40. "name": "地理"
  41. }
  42. ]
  43. },
  44. {
  45. "event_id": 3,
  46. "subjects": [
  47. {
  48. "id": 14,
  49. "name": "生物学"
  50. },
  51. {
  52. "id": 15,
  53. "name": "地理"
  54. },
  55. {
  56. "id": 16,
  57. "name": "历史"
  58. }
  59. ]
  60. }
  61. ];
  62. const allSubjects = data.map(x => x.subjects).reduce((a, x) => [...a, ...x], []);
  63. const uniqueSubjects = allSubjects.filter((e, i) => allSubjects.findIndex(a => a.id === e.id) === i);
  64. console.log(allSubjects, uniqueSubjects);
英文:
  1. const data = [
  2. {
  3. &quot;event_id&quot;: 1,
  4. &quot;subjects&quot;: [
  5. {
  6. &quot;id&quot;: 12,
  7. &quot;name&quot;: &quot;Chemistry&quot;
  8. },
  9. {
  10. &quot;id&quot;: 13,
  11. &quot;name&quot;: &quot;Physics&quot;
  12. },
  13. {
  14. &quot;id&quot;: 14,
  15. &quot;name&quot;: &quot;Psychology&quot;
  16. },
  17. {
  18. &quot;id&quot;: 16,
  19. &quot;name&quot;: &quot;History&quot;
  20. }
  21. ]
  22. },
  23. {
  24. &quot;event_id&quot;: 2,
  25. &quot;subjects&quot;: [
  26. {
  27. &quot;id&quot;: 11,
  28. &quot;name&quot;: &quot;Maths&quot;
  29. },
  30. {
  31. &quot;id&quot;: 12,
  32. &quot;name&quot;: &quot;Chemistry&quot;
  33. },
  34. {
  35. &quot;id&quot;: 14,
  36. &quot;name&quot;: &quot;Biology&quot;
  37. },
  38. {
  39. &quot;id&quot;: 15,
  40. &quot;name&quot;: &quot;Geography&quot;
  41. }
  42. ]
  43. },
  44. {
  45. &quot;event_id&quot;: 3,
  46. &quot;subjects&quot;: [
  47. {
  48. &quot;id&quot;: 14,
  49. &quot;name&quot;: &quot;Biology&quot;
  50. },
  51. {
  52. &quot;id&quot;: 15,
  53. &quot;name&quot;: &quot;Geography&quot;
  54. },
  55. {
  56. &quot;id&quot;: 16,
  57. &quot;name&quot;: &quot;History&quot;
  58. }
  59. ]
  60. }
  61. ];
  62. const allSubjects = data.map(x=&gt; x.subjects).reduce((a,x) =&gt; [...a, ...x] ,[]);
  63. const uniqueSubjects = allSubjects.filter((e,i) =&gt; allSubjects.findIndex(a =&gt; a.id == e.id) == i);
  64. console.log(allSubjects, uniqueSubjects);

huangapple
  • 本文由 发表于 2023年5月29日 23:11:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76358454.html
匿名

发表评论

匿名网友

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

确定