英文:
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 = [
{
"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)
<!-- 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 = [
{
"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)
<!-- 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) => {
accumulator.push(currentValue.subjects.map(x => x.name));
return [...new Set(accumulator.flatMap(x => x))];
}, [])
// will output
// ['Chemistry', 'Physics', 'Psychology', 'History', 'Maths', 'Biology', 'Geography']
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
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"
}
]
}
];
console.log(data.reduce((accumulator, currentValue) => {
accumulator.push(currentValue.subjects.map(x=>x.name));
return [...new Set(accumulator.flatMap(x=>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 = [
{
"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 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);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论