数组截断算法(JavaScript)

题目:

返回一个数组被截断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…

    原文作者:冒泡的马树
    原文地址: https://segmentfault.com/a/1190000019298387
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞