数组截断算法JavaScript

26次阅读

共计 279 个字符,预计需要花费 1 分钟才能阅读完成。

问题:

返回一个数组被截断 n 个元素后还剩余的元素,从索引 0 开始截断。

解答:

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…

正文完
 0