Skip to main content

zipWithIndex

zipWithIndex() function

Returns Iterable/AsyncIterable including an index to the existing Iterable/AsyncIterable value.

Signature:
declare function zipWithIndex<T extends Iterable<unknown> | AsyncIterable<unknown>>(iterable: T): ReturnZipWithIndexType<T>;

Example

const iter = zipWithIndex(["a", "b", "c", "d"]);
iter.next() // {done:false, value: [0, "a"]}
iter.next() // {done:false, value: [1, "b"]}
iter.next() // {done:false, value: [2, "c"]}
iter.next() // {done:false, value: [3, "d"]}
iter.next() // {done:true, value: undefined}

// with pipe
pipe(
["a", "b", "c", "d"],
zipWithIndex,
toArray,
); // [[0, "a"], [1, "b"], [2, "c"], [3, "d"]]

await pipe(
Promise.resolve(["a", "b", "c", "d"]),
zipWithIndex,
toArray,
); // [[0, "a"], [1, "b"], [2, "c"], [3, "d"]]

// with toAsync
await pipe(
[Promise.resolve("a"), Promise.resolve("b"), Promise.resolve("c"), Promise.resolve("d")],
toAsync,
zipWithIndex,
toArray,
); // [[0, "a"], [1, "b"], [2, "c"], [3, "d"]]

Try It

see pipe, toAsync, toArray

Open Source Code