2017-04-25 73 views
2
const newDate = map(items.result, (obj => { 
    if (isDateWithinRage(obj.date_from)) { 
    return { 
     "date": obj.join_date, 
     "name": obj.student.name 
    } 
    } 
})) 

if語句產生了這樣的事情map返回未定義的對象數組?

[Object, Object, Object, Object, Object, undefined, undefined, undefined, undefined, Object, Object, Object] 

如何解決未定義的一部分嗎?我想跳過迭代。

+1

我認爲你正在尋找過濾https://開頭開發商.mozilla.org/EN-US /文檔/網絡/的JavaScript /參考/ Global_Objects /陣列/過濾器 – Maxwelll

回答

2

假設mapArray.prototype.map的一些變體,map產生從輸入數組到輸出數組的1:1映射。

當你想從您的輸入數組排除值,使用Array.prototype.filter

const newDate = 
    items 
    .result 
    .filter(obj => isDateWithinRange(obj.date_from)) 
    .map(obj => ({ 
     date: obj.join_date, 
     name: obj.student.name 
    })); 

此示例假設items.result是一個Array

相關問題