Would you like to clone this notebook?

When you clone a notebook you are able to make changes without affecting the original notebook.

Cancel

_.slice

node v6.17.1
version: 2.0.0
endpointsharetweet
function slice(array, start, end) { // 判断array, start, end 是否存在;并且赋值 let length = array == null ? 0 : array.length; // 如果数组的长度是0 if(!length){ return [] } start = start == null ? 0 : start; end = end == null ? length : end; // 判断start和end是否小于0 if(start < 0) { start = (start + length) > 0 ? (start + length) : 0; } if(end < 0){ end = (end + length) > 0 ? (end + length) : 0; } length = (end - start) > 0 ? ((end - start) >>> 0) : 0; start >>> 0; let index = -1; const result = new Array(length); while(++index < length){ result[index] = array[index + start] } return result; }
slice([1, 2, 3, 4], 0)
slice([1, 2, 3, 4], 0, 3)
slice([1, 2, 3, 4], -1)
slice([1, 2, 3, 4], -2, -1)
Loading…

no comments

    sign in to comment