數組截斷算法(JavaScript)

問題:

返回一個數組被截斷n個元素後還剩餘的元素,從索引0開始截斷。html

解答:

function slasher(arr, howMany) {
// it doesn't always pay to be first
    var arrLength = arr.length;
    var spliceLength = arrLength - howMany;
    var spliceArr;
    
    spliceArr = arr.splice(howMany, spliceLength);
    
    return spliceArr;

}

slasher([1, 2, 3], 2);

連接:

https://www.w3cschool.cn/code...數組

相關文章
相關標籤/搜索