javascript - 在数组中查找具有特定属性的项目,然后计算有多少,然后使用相应结果的对象创建新数组
问题描述
我有一系列问题(如测验),这是结构:
import {SeoCategories} from "../enums/seo";
const initialQuestions = [
{
question: "What is Pogo Sticking?",
category: SeoCategories.analytics,
options: [
{
choice: "The act of visiting a website, then quickly leaving it",
isCorrect: true,
explanation: "This is incorrect due to ...",
},
{
choice: "An SEO Tool",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "A way of Working",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "A website that is down",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What tool can you use to track SEO of a website?",
category: SeoCategories.analytics,
options: [
{
choice: "SEO Spider",
isCorrect: true,
explanation: "This is incorrect due to ..."
},
{
choice: "Geometrix",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Pingdom",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Javascript",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What do you mean by Backlink?",
category: SeoCategories.analytics,
options: [
{
choice: "Incoming Links",
isCorrect: true,
explanation: "The incoming links to your website or webpage are referred to as Backlink. It is also called as an inbound link."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What is the main purpose of using keyword in SEO?",
category: SeoCategories.tools,
options: [
{
choice: "Keywords are used by search engines to populate the subjects over the internet",
isCorrect: true,
explanation: "Search engine stores keywords in the database, and when a search is done, it will come up with the best possible match."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What is keyword stemming?",
category: SeoCategories.tools,
options: [
{
choice: "The process of finding out new keywords",
isCorrect: true,
explanation: "TThe process of finding out new keywords from the root keyword from the search query is referred to as keywords stemming. Adding a prefix, suffix, or pluralization can be used to create the new keyword."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
}
];
如我们所见,这个数组中有 5 个问题。其中 3 个问题属于 ,Analytics
其他 2 个问题属于Tools
。
Analytics
= 5 分中的 3 分。
Tools
= 5 分中的 2 分。
我想要的是通过这一系列问题,并创建一个新数组(或对象),就像这样:["Analytics": 3, "Tools": 2]
所以我可以循环检查各个类别与问题总数,5。
我不确定如何做到这一点。感谢您的时间!
解决方案
您可以reduce
在数组上使用并使用这些键/总数构建一个新对象。
(我添加了该SeoCategories
对象,因为您的问题中缺少该对象)。
const SeoCategories = {
analytics: 'Analytics',
tools: 'Tools'
}
const initialQuestions = [{
question: "What is Pogo Sticking?",
category: SeoCategories.analytics,
options: [{
choice: "The act of visiting a website, then quickly leaving it",
isCorrect: true,
explanation: "This is incorrect due to ...",
},
{
choice: "An SEO Tool",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "A way of Working",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "A website that is down",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What tool can you use to track SEO of a website?",
category: SeoCategories.analytics,
options: [{
choice: "SEO Spider",
isCorrect: true,
explanation: "This is incorrect due to ..."
},
{
choice: "Geometrix",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Pingdom",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Javascript",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What do you mean by Backlink?",
category: SeoCategories.analytics,
options: [{
choice: "Incoming Links",
isCorrect: true,
explanation: "The incoming links to your website or webpage are referred to as Backlink. It is also called as an inbound link."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What is the main purpose of using keyword in SEO?",
category: SeoCategories.tools,
options: [{
choice: "Keywords are used by search engines to populate the subjects over the internet",
isCorrect: true,
explanation: "Search engine stores keywords in the database, and when a search is done, it will come up with the best possible match."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
},
{
question: "What is keyword stemming?",
category: SeoCategories.tools,
options: [{
choice: "The process of finding out new keywords",
isCorrect: true,
explanation: "TThe process of finding out new keywords from the root keyword from the search query is referred to as keywords stemming. Adding a prefix, suffix, or pluralization can be used to create the new keyword."
},
{
choice: "Option 2",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 3",
isCorrect: false,
explanation: "This is incorrect due to ..."
},
{
choice: "Option 4",
isCorrect: false,
explanation: "This is incorrect due to ..."
}
]
}
];
// Iterate over the array
const out = initialQuestions.reduce((acc, c) => {
// Destructure the category from the current answer
// in the iteration
const { category } = c;
// If the category doesn't exist on the accumulator object
// assign it zero, and then add one, otherwise just add one
acc[category] = (acc[category] || 0) + 1;
// Return the accumulator for the next iteration
return acc;
}, {});
console.log(out);
推荐阅读
- typescript - 通过类型谓词赋值时获取编译时错误
- sql - 分组字段以提供范围内的值计数
- asp.net-mvc - Gridmvccore 没有在表中显示任何数据?
- laravel - 只允许购买该产品的人进行评论 - Laravel
- julia - 在 Julia 中旋转数组
- python - Python tkinter - 放置在坐标处时按钮不出现
- java - 用 C++、C#、Java 等编写的桌面 GUI(图形用户界面)是否像所有 Web 或移动 GUI 一样使用 DOM 或类似概念?
- javascript - 在父元素的动画完成后显示一个元素
- json - 如何使用 spark/scala 将 json 字符串格式化为 MongoDB 文档样式?
- mysql - SUM 函数在 MySQL 存储函数中不起作用