2017-08-05 18 views
2

私は質問のリストを返すサービスを呼び出します。それから、私はテーマによってこの質問を再編成する。それは作品オブジェクトの配列を変換する(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": [...] 
} 

ミスコードがないと正しい方法が見つかりません。

提案がありますか? ありがとうございます!

答えて

0

最後の段階で結果セットを取得することで、不足している部分を貪欲にすることができます。

idが一意に見えるので、themeからidにキーを変更しました。

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' } }], 
 
    groupByTheme = questions.reduce(function (obj, item) { 
 
     obj[item.theme.id] = obj[item.theme.id] || []; 
 
     obj[item.theme.id].push(item); 
 
     return obj; 
 
    }, {}), 
 
    result = Object.entries(groupByTheme).map(([id, questions]) => ({ 
 
     id, 
 
     theme: questions[0].theme.label,        // add this 
 
     questions 
 
    })); 
 

 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

ああ、私は愚かなように感じる:)あまりにも長い –

0

私はあなたがそのためにlodashライブラリを使用することをお勧めします。その後、コードはきれいになります。

const result = _.map(_.groupBy(questions, question => question.theme.label), (group, key) => { 
    return {questions: group, theme: key, themeId: themeMap[key]}; 
}); 

:その後、我々のように本当にきれいになりlodashとコードのgroupBymapを使用することができます

const themeMap = {}; 
_.forEach(questions, question => themeMap[question.theme.label] = question.theme.id); 

:私は最初のようlabelからidにマップだろうthemeMapオブジェクトを作りますコードはあまり乱雑です。 ロダッシュを使いたくない場合は、すでに行ったjavascriptのgroupByでreduceを達成することができます。

+0

はい^^同じ問題に焦点を当てた、しかし、私は自分のアプリケーションに一つだけの場合のために、この外部ライブラリを含めたくありませんでした。しかし、ありがとう!多分他の開発者を助けることができます。 –

0

テーマ名で質問をグループ化するときは、{ "themeId": 1, "theme": "dog", "questions": [] }としたい正確な "テーマ"オブジェクトを作成し、小道具に質問をプッシュすることができます。配列に変換するにはObject#valuesを使用します。

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 result = Object.values(// get an array of theme objects 
 
    questions.reduce(function(obj,item){ 
 
     obj[item.theme.label] = obj[item.theme.label] || 
 
     Object.assign({ questions: [] }, item.theme); // create an object that includes theme data, and an empty array for questions 
 

 
     obj[item.theme.label].questions.push(item); // push into questions 
 

 
     return obj; 
 
    }, {}) 
 
); 
 

 
console.log(result);

関連する問題