Returns a new container with the first n elements of the input, preserving the original container type.
n
For AsyncIterable/Iterable the result is lazy and stops the source iterator after n elements. Negative n is treated as 0.
AsyncIterable
Iterable
0
take([1, 2, 3, 4], 2) // [1, 2]take(new Set([1, 2, 3]), 2) // Set { 1, 2 }take({ a: 1, b: 2, c: 3 }, 2) // { a: 1, b: 2 } Copy
take([1, 2, 3, 4], 2) // [1, 2]take(new Set([1, 2, 3]), 2) // Set { 1, 2 }take({ a: 1, b: 2, c: 3 }, 2) // { a: 1, b: 2 }
Returns a new container with the first
nelements of the input, preserving the original container type.For
AsyncIterable/Iterablethe result is lazy and stops the source iterator afternelements. Negativenis treated as0.