私は質問のリストを返すサービスを呼び出します。それから、私はテーマによってこの質問を再編成する。それは作品オブジェクトの配列を変換する(groupbyの並べ替え)
var questions = [
{
id: 1,
label: 'lorem ispum ?',
theme: {
id: 1,
label: 'dog',
}
},
{
id: 2,
label: 'lorem ispum2 ?',
theme: {
id: 2,
label: 'horse',
}
},
{
id: 3,
label: 'lorem ispum3 ?',
theme: {
id: 1,
label: 'dog',
}
},
];
var groupByTheme = questions.reduce(function(obj,item){
obj[item.theme.label] = obj[item.theme.label] || [];
obj[item.theme.label].push(item);
return obj;
}, {});
/*var result = Object.keys(groupsByTheme).map(function(key){
return {theme: key, questions: groupsByTheme[key]};
});
*/
var result = Object.entries(groupByTheme).map(([theme, questions]) => ({ theme, questions }));
console.log(result);
: 私はこの変換を持っています。 しかし、今、私はプロパティを追加したい:テーマのIDです。
{
"themeId": 1,
"theme": "dog",
"questions": [...]
},
{
"themeId": 2,
"theme": "horse",
"questions": [...]
}
ミスコードがないと正しい方法が見つかりません。
提案がありますか? ありがとうございます!
ああ、私は愚かなように感じる:)あまりにも長い –