slice
优质
小牛编辑
140浏览
2023-12-01
Returns a new sequence comprising the portion of this sequence starting from the specified starting index and continuing until the specified ending index or to the end of the sequence.
Signature
ArrayLikeSequence.slice = function(begin, end) { /*...*/ }
ArrayLikeSequence.slice = function slice(begin, end) { var length = this.length(); if (begin < 0) { begin = length + begin; } var result = this.drop(begin); if (typeof end === "number") { if (end < 0) { end = length + end; } result = result.take(end - begin); } return result; }
Name | Type(s) | Description |
---|---|---|
begin | number | The index at which the new sequence should start. |
end | number? | The index at which the new sequence should end. |
returns | ArrayLikeSequence | The new array-like sequence. |
Examples
Lazy([1, 2, 3, 4, 5]).slice(0) // sequence: [1, 2, 3, 4, 5] Lazy([1, 2, 3, 4, 5]).slice(2) // sequence: [3, 4, 5] Lazy([1, 2, 3, 4, 5]).slice(2, 4) // sequence: [3, 4] Lazy([1, 2, 3, 4, 5]).slice(-1) // sequence: [5] Lazy([1, 2, 3, 4, 5]).slice(1, -1) // sequence: [2, 3, 4] Lazy([1, 2, 3, 4, 5]).slice(0, 10) // sequence: [1, 2, 3, 4, 5]