亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

javascript - Encapsulate the repeated parts of these two functions into one function?
大家講道理
大家講道理 2017-05-19 10:09:46
0
3
738
//給數(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?

大家講道理
大家講道理

光陰似箭催人老,日月如移越少年。

reply all(3)
某草草

There is a native method without encapsulation

array.map(item=>item+3)
array.map(item=>encodeURIComponent(item))
左手右手慢動作

The map method that comes with JS has encapsulated your needs

巴扎黑
可以考慮給第二個參數(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;
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template