javascript - 如何根据标准将一组对象映射到另一个对象?
问题描述
如何根据标准将一组对象映射到另一个对象?
如何将以下问题数组转换为预期数组?
const questions = [
{ type: 'question', index: 1 },
{ type: 'question', index: 2 },
{ type: 'answer', index: 3 },
{ type: 'answer', index: 4 },
{ type: 'answer', index: 5 },
{ type: 'answer', index: 6 },
{ type: 'answer', index: 7 },
{ type: 'answer', index: 8 },
{ type: 'answer', index: 9 },
{ type: 'question', index: 11 },
{ type: 'answer', index: 12 },
{ type: 'answer', index: 13 },
{ type: 'question', index: 14 },
{ type: 'question', index: 15 },
{ type: 'question', index: 16 },
{ type: 'question', index: 17 },
{ type: 'answer', index: 18 },
{ type: 'answer', index: 19 },
{ type: 'question', index: 20 },
{ type: 'question', index: 21 },
{ type: 'question', index: 22 },
{ type: 'question', index: 23 },
{ type: 'question', index: 24 },
{ type: 'question', index: 25 },
{ type: 'question', index: 26 },
{ type: 'question', index: 27 },
{ type: 'question', index: 28 },
{ type: 'question', index: 100 },
{}
];
这是预期的结果数组。
const expected = [
{ type: "question", index: 1, answers: [] },
{
type: "question",
index: 2,
answers: [
{ type: "answer", index: 3 },
{ type: "answer", index: 4 },
{ type: "answer", index: 5 },
{ type: "answer", index: 6 },
{ type: "answer", index: 7 },
{ type: "answer", index: 8 },
{ type: "answer", index: 9 },
],
},
{
type: "question",
index: 11,
answers: [
{ type: "answer", index: 12 },
{ type: "answer", index: 13 },
],
},
{ type: "question", index: 14, answers: [] },
{ type: "question", index: 15, answers: [] },
{ type: "question", index: 16, answers: [] },
{
type: "question",
index: 17,
answers: [
{ type: "answer", index: 18 },
{ type: "answer", index: 19 },
],
},
{ type: "question", index: 20, answers: []},
{ type: "question", index: 21, answers: []},
{ type: "question", index: 22, answers: []},
{ type: "question", index: 23, answers: []},
{ type: "question", index: 24, answers: []},
{ type: "question", index: 25, answers: []},
{ type: "question", index: 26, answers: []},
{ type: "question", index: 27, answers: []},
{ type: "question", index: 28, answers: []},
{ type: 'question', index: 100, answers: [] },
{}
];
所以想法是调用一个子数组answers
,我们用type === 'answer'
.
所以答案应该被推送到索引n-1
,type === 'question'
直到type === 'question'
遇到下一个具有类型的项目。如果中间没有答案,则保持数组为空。
基本上我们寻找索引和类型
更新
所以我尝试的方法是创建一个包含索引的数组。每次循环时,我都会寻找下一个最大索引及其类型。后来基于此,我尝试将中间索引复制到前一个索引。
解决方案
我想你可以reduce
在这里使用。这就是我的想法。
const questions = []; // your questions here
questions.reduce((acc, question) => {
if (question.type === 'question') return [...acc, Object.assign({}, question, { answers: [] })];
else if (question.type === 'answer') {
return [
...acc.slice(0, acc.length-1),
Object.assign({}, acc[acc.length-1], {
answers: [...acc[acc.length-1].answers, question]
})
];
}
}, []);
好吧,代码看起来不漂亮而且不可读。questions
如果可以使代码更漂亮,我建议更改您的数据结构。
推荐阅读
- python - 使用 json 从节点到 python 的通信给了我错误
- java - Java、Maven、JSP、JPA - java.lang.NumberFormatException:对于输入字符串:“id”
- c# - 为什么在 .NET 方法注释中使用数字字符引用?
- c - PIE 文件在链接期间是否提供了虚拟内存地址?
- json - 根据 JSON 标准对某些 tableView 行应用复选标记
- android-studio - 如何阻止 AndroidStudio 覆盖 .idea/codeStyles/Project.xml
- python - Python Pandas:如何使用返回 pd.Series 的函数进行分组聚合
- swift - 如何在 segue 期间将数据传递给 2 个或更多 ViewController
- reactjs - 带有 useState 的 Radium 使钩子调用无效
- html - 可以在 HTML 元素中识别角度组件吗?