根据另一个数组中的项目创建一个新数组

时间:2018-07-01 20:06:50

标签: javascript arrays

我有一个对象数组,该对象数组是mongoDB查询的结果,该查询显示了用户尝试测验的结果,并将其分配给变量res.locals.spellingTestResults。每个用户可以尝试多次。

[{
        _id: {
            user: {
                name: 'PageHill Class1 Child',
                userType: 'Child',
                email: '5ab79cc913840a359ff425f1@empty.com',
                __v: 0
            }
        },
        data: [{
                numberOfSpellings: 4,
                wrongAnswers: ['of', 'spellings'],
                rightAnswers: 2,
                score: 2
            },
            {
                numberOfSpellings: 4,
                wrongAnswers: ['spellings'],
                rightAnswers: 3,
                score: 3
            }
        ]
    },
    {
        _id: {
            user: {
                name: 'PageHillAdmin',
                userType: 'Teacher',
                email: 'jinky332@gmail.com',
                __v: 0
            }
        },
        data: [{
                numberOfSpellings: 4,
                wrongAnswers: ['first','spellings'],
                rightAnswers: 0,
                score: 3
            },
            {
                numberOfSpellings: 4,
                wrongAnswers: ['of', 'spellings'],
                rightAnswers: 0,
                score: 4
            }
        ]
    }
];

由此,我希望能够创建一个对象数组(如上所述,每个用户一个),其中包含属于它们的每个wrongAnswers数组的串联。我想计算结果数组中每个项目的实例。结果看起来像下面的样子:

[{

    user: {
        name: 'PageHill Class1 Child'
    }

    wrongAnswers: {
        'spellings': 2,
        'of': 1
    },

    user: {
        name: 'PageHillAdmin'
    },
    wrongAnswers: {
        'spellings': 2,
        'first': 1,
        'of': 1
    }

}];

我确实有一些单独的逻辑来组合所有用户的所有错误答案:

        let allWrongAnswers = [];
        res.locals.spellingTestResults.map(function(answers) {    
            answers.data.forEach(answer => {
                allWrongAnswers = allWrongAnswers.concat(answer.wrongAnswers);
            });
        });

        var countedSpellings = allWrongAnswers.reduce(function (allSpellings, spelling) { 
        if (spelling in allSpellings) {
            allSpellings[spelling]++;
        }
        else {
            allSpellings[spelling] = 1;
        }
        return allSpellings;
        }, {});

但是我不知道如何为每个用户合并并计算错误答案。

2 个答案:

答案 0 :(得分:0)

如果要把一堆东西变成一堆(用户结果=> totals对象),请使用array reduce:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

尝试使用零值作为初始值来定义理想的计数对象,然后将其添加到reduce函数中。

答案 1 :(得分:0)

尝试一下此脚本:

const dataFromMongo = [
  {
    _id: {
      user: {
        name: 'PageHill Class1 Child',
        userType: 'Child',
        email: '5ab79cc913840a359ff425f1@empty.com',
        __v: 0,
      },
    },
    data: [
      {
        numberOfSpellings: 4,
        wrongAnswers: ['of', 'spellings'],
        rightAnswers: 2,
        score: 2,
      },
      {
        numberOfSpellings: 4,
        wrongAnswers: ['spellings'],
        rightAnswers: 3,
        score: 3,
      },
    ],
  },
  {
    _id: {
      user: {
        name: 'PageHillAdmin',
        userType: 'Teacher',
        email: 'jinky332@gmail.com',
        __v: 0,
      },
    },
    data: [
      {
        numberOfSpellings: 4,
        wrongAnswers: ['first', 'spellings'],
        rightAnswers: 0,
        score: 3,
      },
      {
        numberOfSpellings: 4,
        wrongAnswers: ['of', 'spellings'],
        rightAnswers: 0,
        score: 4,
      },
    ],
  },
];

const result = dataFromMongo.reduce(
  (acc, item) => [
    ...acc,
    {
      user: item._id.user.name,
      wrongAnswers: item.data
        .reduce((acc, item) => [...acc, ...item.wrongAnswers], [])
        .reduce((acc, wrongAnswer) => {
          return {
            ...acc,
            [wrongAnswer]: acc[wrongAnswer] ? acc[wrongAnswer] + 1 : 1,
          };
        }, {}),
    },
  ],
  []
);

console.log('result:', result);