在数组中查找具有特定属性的项,然后计算数量,然后创建具有相应结果的对象的新数组



我有一系列问题(比如测验(,这是结构:

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

CCD_ 3=5中的3。CCD_ 4=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);

这应该使任务:

initialQuestions.reduce((a,b) => {a[b.category]? a[b.category]++:(a[b.category] = 1);return a}, {});
console.log(res);

const SeoCategories = {analytics: "Analitics", 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 ..."
}
]
}
];
const res = initialQuestions.reduce((a,b) => {a[b.category]? a[b.category]++:(a[b.category] = 1);return a}, {});
console.log(res);

相关内容

最新更新