我有對象的數組:ES6映射對象的數組,返回對象的數組新的密鑰
[
{
id: 1,
name: 'bill'
},
{
id: 2,
name: 'ted'
}
]
尋找一個簡單的一行返回:
[
{
value: 1,
text: 'bill'
},
{
value: 2,
text: 'ted'
}
]
所以我可以用適當的按鍵輕鬆地將它們泵入反應下拉列表。
我覺得像這樣的簡單的解決辦法應該工作,但我發現無效語法錯誤:
this.props.people.map(person => { value: person.id, text: person.name })
你需要'person =>({...'。換句話說,將對象字面值包含在parens中,否則JS會認爲它是塊的開始部分 –