首页 > 解决方案 > 如何将特定问题分类到特定部分?

问题描述

我从 API 获取数据,并且能够将从 API 接收到的大数据分类到两个数组中。一个称为 section 的数组和一个称为 questions 的数组。所以sections数组看起来像这样:

const section = [section-1: some_title, section-2:some_title, section-3:some_title] 问题数组如下所示:

常量问题 = [Q1-1:some_question, Q1-2:some_question, Q1-3:some_question, Q2-1:some_question, Q2-2:some_question, Q2-3:some_question, Q3-1:some_question, Q3-2: some_question,Q3-3:some_question]

我想要做的是将两个数组合并在一起并对它们进行排序: const MergedArray = [{section-1: some_title, questions: [ Q1-1:some_question,Q1-2:some_question,Q1-3:some_question]},{section-2:some_title, questions: [Q2-1:some_question,Q2-2:some_question,Q2-3:some_question]},{section-3:some_title, questions: [Q3-1:some_question,Q3-2:some_question,Q3-3:some_question]}] 任何帮助将不胜感激

标签: javascriptarraysreactjs

解决方案


从您添加的评论中,我希望section,questions和的结构MergedArray如下

const section =  [
    'section-1:some_title',
    'section-2:some_title',
    'section-3:some_title',
];
const questions = [
    'Q1-1:some_question',
    'Q1-2:some_question',
    'Q1-3:some_question',
    'Q2-1:some_question',
    'Q2-2:some_question',
    'Q2-3:some_question',
    'Q3-1:some_question',
    'Q3-2:some_question',
    'Q3-3:some_question',
]
const MergedArray = [
    {
        'section-1': 'some_title',
        questions: [ 'Q1-1:some_question','Q1-2:some_question','Q1-3:some_question']
    },
    {
        'section-2':'some_title',
        questions: ['Q2-1:some_question','Q2-2:some_question','Q2-3:some_question']
    },
    {
        'section-3':'some_title',
        questions: ['Q3-1:some_question','Q3-2:some_question','Q3-3:some_question']
    }
];

请在下面找到生成它们的解决方案。我已经为代码功能添加了注释。

const section = [
    'section-1:some_title',
    'section-2:some_title',
    'section-3:some_title',
];
const questions = [
    'Q1-1:some_question',
    'Q1-2:some_question',
    'Q1-3:some_question',
    'Q2-1:some_question',
    'Q2-2:some_question',
    'Q2-3:some_question',
    'Q3-1:some_question',
    'Q3-2:some_question',
    'Q3-3:some_question',
]

const MergedArray = section.reduce((accumulator, currentValue) => {
    const node = {};

    const [key, value] = currentValue.split(':');
    // This will generate
    // key = 'section-1' and value='some_title' for first itration
    // key = 'section-2' and value='some_title' for second itration
    // key = 'section-3' and value='some_title' for third itration

    node[key] = value;
    // this will update node object as
    // node = {'section-1': 'some_title'} for first itration
    // node = {'section-2': 'some_title'} for second itration
    // node = {'section-3': 'some_title'} for third itration

    const sectionIndex = key.split('-')[1];
    // sectionIndex will be
    // 1 for first itration
    // 2 for second itration
    // 3 for third itration

    // Now you have to pick questions from questions array.
    // Questions For section-1 are 'Q1-1:some_question', 'Q1-2:some_question', 'Q1-3:some_question',
    // Questions For section-2 are 'Q2-1:some_question', 'Q2-2:some_question', 'Q2-3:some_question',
    // Questions For section-3 are 'Q3-1:some_question', 'Q3-2:some_question', 'Q3-3:some_question',
    // So you have to flter out the questions from the array.
    // `Q${sectionIndex}-` is string manipulation. This will generate 'Q1-', 'Q2-', 'Q3-'.
    // You have to filter out questions staring with these strings.
    // i.e, their respective index should be zero in the matching words from that array
    // The below expression will filter out questions starting with
    // 'Q1-' in the first itration
    // 'Q2-' in the second itration
    // 'Q3-' in the third itration
    const questionList = questions.filter((ques) => ques.indexOf(`Q${sectionIndex}-`) === 0);
    // this will make
    // questionList = [ 'Q1-1:some_question', 'Q1-2:some_question', 'Q1-3:some_question'] in the first itration
    // questionList = [ 'Q2-1:some_question', 'Q2-2:some_question', 'Q2-3:some_question'] in the second itration
    // questionList = [ 'Q3-1:some_question', 'Q3-2:some_question', 'Q3-3:some_question'] in the third itration

    node.questions = questionList;
    // This will update node as
    // node = {'section-1': 'some_title' questions: [ 'Q1-1:some_question', 'Q1-2:some_question', 'Q1-3:some_question']} in the first itration
    // node = {'section-2': 'some_title' questions: [ 'Q2-1:some_question', 'Q2-2:some_question', 'Q2-3:some_question']} in the second itration
    // node = {'section-3': 'some_title' questions: [ 'Q3-1:some_question', 'Q3-2:some_question', 'Q3-3:some_question']} in the third itration

    accumulator.push(node);
    return accumulator;
}, []);
console.log(MergedArray);


推荐阅读