首页 > 解决方案 > 在数组中查找具有特定属性的项目,然后计算有多少,然后使用相应结果的对象创建新数组

问题描述

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

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。

我不确定如何做到这一点。感谢您的时间!

标签: javascript

解决方案


您可以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);


推荐阅读