当前位置: 首页 > 文档资料 > Lazy.js 英文文档 >

slice

优质
小牛编辑
133浏览
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;
}
NameType(s)Description
beginnumber

The index at which the new sequence should start.

endnumber?

The index at which the new sequence should end.

returnsArrayLikeSequence

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]