当前位置:网站首页>Handwritten array.property.map

Handwritten array.property.map

2021-09-15 08:12:31 hustersong

The front end understands arrays map Method

function map(arr,callBack){
  //  First judgement arr Is it an array ,callBack Whether it is function, If not, return the empty array directly 
  if(!Array.isArray(arr) || !arr.length || typeof callBack != 'function') return []
  else {
    var result = []
    //  Traversal array arr
    for(let i=0,len =arr.length;i<len;i++)
    {
        //  Change the current page to i Ergodic arr[i],i, And arrays arr Passed as a formal parameter to the callback function , And store it in an empty array result
        result.push(callBack(arr[i],i,arr))
    }
    // And then the array result return 
    return result
  }
}
var arr985 = [9, 8, 5]
map(arr985,(item,index,arr)=>{
    console.log(item)
    console.log(index)
    console.log(arr)
    console.log(item * 2)
    return item*10
})
arr985.map((item,index,arr)=>{
    console.log(item)
    console.log(index)
    console.log(arr)
    console.log(item * 2)
    return item*10;
})

版权声明
本文为[hustersong]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/09/20210909134740257Y.html

随机推荐