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

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

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

问题

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

[
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 13,
        "name": "物理"
      },
      {
        "id": 14,
        "name": "心理学"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "数学"
      },
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  }
]

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

英文:

I had an object with this structure:

[
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 13,
        "name": "Physics"
      },
      {
        "id": 14,
        "name": "Psychology"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "Maths"
      },
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  }
]

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.

const data = [
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 13,
        "name": "Physics"
      },
      {
        "id": 14,
        "name": "Psychology"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "Maths"
      },
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  }
]

const all = []
for (const { subjects } of data) {
  subjects.forEach((s) => {
    if (all.indexOf(s.name) === -1) {
      all.push(s.name)
    }
  })
}
console.log(all)

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

const data = [
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 13,
        "name": "Physics"
      },
      {
        "id": 14,
        "name": "Psychology"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "Maths"
      },
      {
        "id": 12,
        "name": "Chemistry"
      },
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "Biology"
      },
      {
        "id": 15,
        "name": "Geography"
      },
      {
        "id": 16,
        "name": "History"
      }
    ]
  }
]

const all = []
for (const { subjects } of data) {
  subjects.forEach((s) => {
    all.push(s.name)
  })
}
const s = [...new Set(all)]
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 -->

const data = [
{
&quot;event_id&quot;: 1,
&quot;subjects&quot;: [
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 13,
&quot;name&quot;: &quot;Physics&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Psychology&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
},
{
&quot;event_id&quot;: 2,
&quot;subjects&quot;: [
{
&quot;id&quot;: 11,
&quot;name&quot;: &quot;Maths&quot;
},
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
}
]
},
{
&quot;event_id&quot;: 3,
&quot;subjects&quot;: [
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
}
]
const all = []
for (const { subjects } of data) {
subjects.forEach((s) =&gt; {
if (all.indexOf(s.name) === -1) {
all.push(s.name)
}
})
}
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 -->

const data = [
{
&quot;event_id&quot;: 1,
&quot;subjects&quot;: [
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 13,
&quot;name&quot;: &quot;Physics&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Psychology&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
},
{
&quot;event_id&quot;: 2,
&quot;subjects&quot;: [
{
&quot;id&quot;: 11,
&quot;name&quot;: &quot;Maths&quot;
},
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
}
]
},
{
&quot;event_id&quot;: 3,
&quot;subjects&quot;: [
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
}
]
const all = []
for (const { subjects } of data) {
subjects.forEach((s) =&gt; {
all.push(s.name)
})
}
const s = [...new Set(all)]
console.log(s)

<!-- end snippet -->

答案2

得分: 0

以下是代码的翻译部分:

data.reduce((accumulator, currentValue) => {
    accumulator.push(currentValue.subjects.map(x => x.name));
    return [...new Set(accumulator.flatMap(x => x))];
}, [])

// 输出结果
// ['化学', '物理', '心理学', '历史', '数学', '生物学', '地理学']
const data = [
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 13,
        "name": "物理"
      },
      {
        "id": 14,
        "name": "心理学"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "数学"
      },
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理学"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理学"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  }
];

console.log(data.reduce((accumulator, currentValue) => {
    accumulator.push(currentValue.subjects.map(x => x.name));
    return [...new Set(accumulator.flatMap(x => x))];
}, []));

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

英文:

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

data.reduce((accumulator, currentValue) =&gt; {
accumulator.push(currentValue.subjects.map(x =&gt; x.name));
return [...new Set(accumulator.flatMap(x =&gt; x))];
}, [])
// will output
// [&#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 -->

const data = [
{
&quot;event_id&quot;: 1,
&quot;subjects&quot;: [
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 13,
&quot;name&quot;: &quot;Physics&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Psychology&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
},
{
&quot;event_id&quot;: 2,
&quot;subjects&quot;: [
{
&quot;id&quot;: 11,
&quot;name&quot;: &quot;Maths&quot;
},
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
}
]
},
{
&quot;event_id&quot;: 3,
&quot;subjects&quot;: [
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
}
];
console.log(data.reduce((accumulator, currentValue) =&gt; {
accumulator.push(currentValue.subjects.map(x=&gt;x.name));
return [...new Set(accumulator.flatMap(x=&gt;x))];
}, []));

<!-- end snippet -->

答案3

得分: 0

const data = [
  {
    "event_id": 1,
    "subjects": [
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 13,
        "name": "物理"
      },
      {
        "id": 14,
        "name": "心理学"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  },
  {
    "event_id": 2,
    "subjects": [
      {
        "id": 11,
        "name": "数学"
      },
      {
        "id": 12,
        "name": "化学"
      },
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理"
      }
    ]
  },
  {
    "event_id": 3,
    "subjects": [
      {
        "id": 14,
        "name": "生物学"
      },
      {
        "id": 15,
        "name": "地理"
      },
      {
        "id": 16,
        "name": "历史"
      }
    ]
  }
];

const allSubjects = data.map(x => x.subjects).reduce((a, x) => [...a, ...x], []);
const uniqueSubjects = allSubjects.filter((e, i) => allSubjects.findIndex(a => a.id === e.id) === i);

console.log(allSubjects, uniqueSubjects);
英文:
const data = [
{
&quot;event_id&quot;: 1,
&quot;subjects&quot;: [
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 13,
&quot;name&quot;: &quot;Physics&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Psychology&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
},
{
&quot;event_id&quot;: 2,
&quot;subjects&quot;: [
{
&quot;id&quot;: 11,
&quot;name&quot;: &quot;Maths&quot;
},
{
&quot;id&quot;: 12,
&quot;name&quot;: &quot;Chemistry&quot;
},
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
}
]
},
{
&quot;event_id&quot;: 3,
&quot;subjects&quot;: [
{
&quot;id&quot;: 14,
&quot;name&quot;: &quot;Biology&quot;
},
{
&quot;id&quot;: 15,
&quot;name&quot;: &quot;Geography&quot;
},
{
&quot;id&quot;: 16,
&quot;name&quot;: &quot;History&quot;
}
]
}
];
const allSubjects = data.map(x=&gt; x.subjects).reduce((a,x) =&gt; [...a, ...x] ,[]);
const uniqueSubjects = allSubjects.filter((e,i) =&gt; allSubjects.findIndex(a =&gt; a.id == e.id) == i);
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:

确定