//給數(shù)組中的每一項增加3
function addThreeForEachItem(array){
for(var i = 0; i < array.length; i++){
array[i] = array[i] + 3;
}
return array;
}
//將數(shù)組中的每一項編碼
function encodeEachItem(array){
for(var i = 0; i < array.length; i++){
array[i] = encodeURIComponent(array[i]);
}
return array;
}
The two functions are very similar (duplicate). How to encapsulate the repeated parts of these two functions into one function?
光陰似箭催人老,日月如移越少年。
There is a native method without encapsulation
array.map(item=>item+3)
array.map(item=>encodeURIComponent(item))
可以考慮給第二個參數(shù)來區(qū)分
function repeatControl(array,param){
for(var i = 0; i < array.length; i++){
if(param=='addThree'){
array[i] + = 3;
}else{
array[i] = encodeURIComponent(array[i]);
}
}
return array;
}