map()函数根据一个回调函数创建一个新的函数,它会遍历数组中的每一个值
例子
var arr1 = [1,3,5,6,8,19]
const map1 = arr1.map(function(singleEl){
return singleEl*2;
})
console.log(map1)
//返回的map1函数数组为[2,6,10,12,16,38]
回调函数可用的参数
var new_array = arr.map(function callback(currentValue[,index[,array]]){
//返回数组中的单个元素
},thisArg)
参数:
callback:
生成新数组的函数,其中有三个参数:
currentValue:
回调中正在处理的当前元素
index (可选)
回调中处理的当前元素在源数组中的索引
array (可选)
源数组
thisArg
回调中使用的this值
应用
vue中重复渲染组件
<div id='app'>
<ele></ele>
</div>
<script>
var child = {
render:function(createElement){
return createElement('p','文字');
}
};
Vue.component('ele',{
render:function(createElement){
return createElement('div',
Array.apply(null,{
length:5
}).map(function(){
return createElement(Child);
})
);
}
});
var app = new Vue({
el:'#app'
})
</script>