[英]Array map returning array of undefined when should return array of objects
為什么
['a', 'b', 'c'].map((x) => { letter: x })
返回一個undefined數組
和
['a', 'b', 'c'].map((x) => [{ letter: x }][0])
正確返回一個對象數組?
你需要在()
包裝對象
var result = ['a', 'b', 'c'].map((x) => ({ letter: x })) console.log(result)
缺少顯式return語句的函數將返回undefined。 () => {}
等同於function(){}
。 x => x
相當於function(x){ return x;}
因此,沒有{}
箭頭函數將返回表達式的計算值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.