這是輸出(圖片1),如果你將記錄final
陣列從片段1ES6減少方法不返回所需的輸出
我試圖做的是減少我的數組像所顯示的陣列中圖像2
我已經使用lodash _.uniqBy()
[試圖片段2],但仍然輸出,我從日誌記錄reduce
變量仍然是輸出從圖片1。如何將我的final
陣列減少到輸出圖片2?
圖像2
const reduce = final.reduce((x, y) => {
if (x.department !== y.department) {
return x;
}
})
console.log(final)
console.log(reduce);
片段1
_.uniqBy(final, 'department')
片段2
UPDATE
這是打字稿.ts
不.js
。
接口=>[{ department: string, professors: string[] }]
至於請求
這是我得到我的數據
[
{
department: 'Department of Mathematics - INTRAMUROS',
professors: [
'Sabino, Lilibeth D.',
'Department Chair',
.....
這是我的代碼從我final
陣列到達之前
const recommend = [];
const final = [];
recommended.forEach((prof) => {
const find = proffessorDetails.find((collection) => {
return collection.professors.find((professor) => professor == prof);
});
recommend.push(find);
})
const find = recommend.map((collection) => {
return {
department: collection.department,
prof: _.intersection(collection.professors, recommended)
};
});
final.push(find);
你總是返回累加器減少功能 – MinusFour
這不是你如何使用'reduce'對象。 – str
我明白了,那麼有什麼可能是最好的解決方案,以減少我的最終陣列到圖像2的輸出? – Vadamadafaka