2017-08-05 69 views
2

我打電話給返回問題列表的服務。然後,我按主題重新組合這個問題。 我有這樣的轉換:轉換對象數組(每組的對象)

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": [...] 
} 

我沒有找到正確的方式,沒有一個missy代碼。

有什麼建議嗎? 在此先感謝!

回答

0

您可以通過在最後階段獲取結果集來填充缺失的部分。

我將密鑰從theme更改爲id,因爲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庫這一點。那麼代碼將非常乾淨。我會先作themeMap對象這將是地圖從labelid爲:

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

然後我們可以使用lodash的groupBymap和代碼將非常光潔如:

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

代碼將不那麼混亂。 如果你不想使用lodash,那麼你可以通過你已經做過的javascript的reduce來實現groupBy。

+0

是的,但我不希望這包括外部庫在我的應用程序只是一個案例。但是,謝謝!它可以幫助其他開發者。 –

0

當您按主題名稱對問題進行分組時,您可以創建您想要的確切「主題」對象{ "themeId": 1, "theme": "dog", "questions": [] },並將問題推送到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);