I have an array of questions (like quizzes), this is the structure:
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 ..."
}
]
}
];
As we can see, we have 5 questions in this array. 3 of these questions have the category of Analytics
and then other 2 questions have the category of Tools
.
Analytics
= 3 out of 5.
Tools
= 2 out of 5.
What I want, is to go through this array of questions, and create a new array (or object), that is something like this: ["Analytics": 3, "Tools": 2]
So i can loop through can check the respective category vs total amount of questions, 5.
I am unsure of how to do this. Thank you for your time!
2
Answers
You can use
reduce
on the array and build a new object with those keys/totals.(I added the
SeoCategories
object because that was missing from your question).This should make the task: