У меня проблемы с отображением объекта JS и объединением данных из одного параметра в подобъект другого параметра.
Предположим, у меня есть следующее JSON:
const data = [
{
"difficulty": "Easy",
"levels": [
{
"id": 0,
"title": "Training",
"description": "First training level."
},
{
"id": 1,
"title": "Story",
"description": "First story level."
}
]
},
{
"difficulty": "Medium",
"levels": [
{
"id": 2,
"title": "Training",
"description": "First training level at medium difficulty."
}
]
}
];
И я хочу манипулировать этим, чтобы вернуть:
[
{
"id": 0,
"title": "Training",
"description": "First training level.",
"difficulty": "Easy"
},
{
"id": 1,
"title": "Story",
"description": "First story level.",
"difficulty": "Easy"
},
{
"id": 2,
"title": "Training",
"description": "First training level at medium difficulty.",
"difficulty": "Medium"
}
];
Я пытался:
const result = data.map(item => {
const { difficulty, levels } = item;
return levels.map(level => ({ ...level, difficulty }));
});
Но я получаю:
[
[
{
"id": 0,
"title": "Training",
"description": "First training level.",
"difficulty": "Easy"
},
{
"id": 1,
"title": "Story",
"description": "First story level.",
"difficulty": "Easy"
}
],
[
{
"id": 2,
"title": "Training",
"description": "First training level at medium difficulty.",
"difficulty": "Medium"
}
]
]
Я хочу вернуть только levels
с добавлением difficulty
.