zipWithIndex() function
返回在现有 Iterable/AsyncIterable 值中包含索引的 Iterable/AsyncIterable。
Signature:
typescript
declare function zipWithIndex<
T extends Iterable<unknown> | AsyncIterable<unknown>,
>(iterable: T): ReturnZipWithIndexType<T>;Example
ts
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"]]