let arr = [] let newArr = arr.map((item, i, arr) => { //item:遍歷數組的每一項,i:數組當前項的下標,arr原數組 console.log(`item---${item}, i---${i}, arr---${arr}`) return item + i }) console.log(newArr) //[]
**函數內console沒有執行,證實數組爲空是並不執行遍歷數組
let arr = [1, 2, 3] let newArr = arr.map((item, i, arr) => { //item:遍歷數組的每一項,i:數組當前項的下標,arr原數組 console.log(`item---${item}, i---${i}, arr---${arr}`) return }) console.log(newArr) //[undefined, undefined, undefined]
**即使函數返回空 結果數組的長度和原數組是一致的函數
let arr = [1, 2, 3] let newArr = arr.map((item, i, arr) => { //item:遍歷數組的每一項,i:數組當前項的下標,arr原數組 console.log(`item---${item}, i---${i}, arr---${arr}`) return item + i }) console.log(newArr,arr) //[1, 3, 5]
let arr = [1, 2, 3] let obj = { 1: 2 } let arr1 = [1, 2] let str = 123 // let newArr = arr.map(obj) let newArr1 = arr.map(arr1) let newArr2 = arr.map(str)
函數接收三個參數spa
let arr = [1, 2, 3] let newArr = arr.map((item, i, arr) => { //item:遍歷數組的每一項,i:數組當前項的下標,arr原數組 console.log(`item---${item}, i---${i}, arr---${arr}`) return item + i }) console.log(newArr) //[1, 3, 5]
您的點贊是我繼續下去的動力!code
歡迎多多吐槽指正! 謝謝!blog