2016-12-06 23 views
1

所以我有一個用於/ in循環來變換對象爲對象的數組中的一個陣營預輸入組件使用:如何將其重構爲ES6?

const languageOptions = [] 
for (const language in i18n.store.data[i18n.language].translation.languages) { 
    languageOptions.push({ 
    id: language, 
    label: i18n.store.data[i18n.language].translation.languages[language], 
    }) 
} 

不過,我得到一個eslint錯誤:Using 'ForInStatement' is not allowed (no-restricted-syntax)

我如何預計現在會進行這種轉變?

回答

1

Object.keysObject.entries

const languageOptions = 
    Object.entries(i18n.store.data[i18n.language].translation.languages) 
    .map(([id, label]) => ({id, label})) 

假設你不需要枚舉的屬性更遠了原型鏈。如果是這樣,請關閉ESLint規則。 =)