怎么利用网站做外链接,wordpress 漂亮的博客,wordpress 页面 锚,wordpress导航菜单的下拉菜单JavaScript中的map()方法详解
map()方法经常拿来遍历数组#xff0c;但是不改变原数组#xff0c;但是会返回一个新的数组#xff0c;并且这个新的数组不会改变原数组的长度 注意#xff1a;有时候会出现这种现象#xff0c;出现几个undefined const array [1, 4,9, 16…JavaScript中的map()方法详解
map()方法经常拿来遍历数组但是不改变原数组但是会返回一个新的数组并且这个新的数组不会改变原数组的长度 注意有时候会出现这种现象出现几个undefined const array [1, 4,9, 16]console.log(原数组array为,array)const map array.map(x {if (x 5) {return x }})//返回[undefined,undefined,9,16]其实map方法是对每一项数组进行遍历遍历一次返回一个值给新数组加上一个元素这是就是满足x4的元素只有两个所以其他项就返回了undefined。
map方法的实现 Array.prototype.fakeMap function(fn,context) {let arr this;let temp [];for(let i0;iarr.length;i){let result fn.call(context,arr[i],i,arr);temp.push(result);}return temp;}