Skip to main content
This release is 5 versions behind 1.33.0 — the latest version of @es-toolkit/es-toolkit. Jump to latest

@es-toolkit/es-toolkit@1.29.0-dev.958+1d1d0ee6
Built and signed on GitHub Actions

A modern JavaScript utility library that's 2-3 times faster and up to 97% smaller—a major upgrade to lodash.

This package works with Cloudflare Workers, Node.js, Deno, Bun, Browsers
This package works with Cloudflare Workers
This package works with Node.js
This package works with Deno
This package works with Bun
This package works with Browsers
JSR Score
100%
Published
3 months ago (1.29.0-dev.958+1d1d0ee6)
function forEach
forEach<T>(
array: T[],
callback?: (
value: T,
index: number,
array: T[],
) => unknown
,
): T[]

Iterates over each element of the array invoking the provided callback function for each element.

Examples

Example 1

forEach([1, 2, 3], (value, index, array) => console.log(value, index)); // Output: // 1 0 // 2 1 // 3 2

Type Parameters

  • The type of the array.

Parameters

array: T[]
  • The array to iterate over.
optional
callback: (
value: T,
index: number,
array: T[],
) => unknown
  • The function invoked for each element. The callback function receives three arguments:
  • 'value': The current element being processed in the array.
  • 'index': The index of the current element being processed in the array.
  • 'array': The array 'forEach' was called upon.

Return Type

T[]

Returns the original array.

forEach<T>(
array: readonly T[],
callback?: (
value: T,
index: number,
array: T[],
) => unknown
,
): readonly T[]

Iterates over each element of the array invoking the provided callback function for each element.

Examples

Example 1

forEach([1, 2, 3], (value, index, array) => console.log(value, index)); // Output: // 1 0 // 2 1 // 3 2

Type Parameters

  • The type of the array.

Parameters

array: readonly T[]
  • The array to iterate over.
optional
callback: (
value: T,
index: number,
array: T[],
) => unknown
  • The function invoked for each element. The callback function receives three arguments:
  • 'value': The current element being processed in the array.
  • 'index': The index of the current element being processed in the array.
  • 'array': The array 'forEach' was called upon.

Return Type

readonly T[]

Returns the original array.

forEach<T extends
string
| null
| undefined
>
(
string: T,
callback?: (
char: string,
index: number,
string: string,
) => unknown
,
): T

Iterates over each element of the array invoking the provided callback function for each element.

Examples

Example 1

forEach('abc', (char, index, string) => console.log(char, index)); // Output: // 'a' 0 // 'b' 1 // 'c' 2

Type Parameters

T extends
string
| null
| undefined
  • The type of string.

Parameters

string: T
  • The string to iterate over
optional
callback: (
char: string,
index: number,
string: string,
) => unknown
  • The function invoked for each char. The callback function receives three arguments:
  • 'char': The current char being processed in the string.
  • 'index': The index of the current char being processed in the string.
  • 'string': The string 'forEach' was called upon.

Return Type

Returns the original string.

forEach<T>(
array: ArrayLike<T>,
callback?: (
value: T,
index: number,
array: ArrayLike<T>,
) => unknown
,
): ArrayLike<T>

Iterates over each element of the array invoking the provided callback function for each element.

Examples

Example 1

forEach([1, 2, 3], (value, index, array) => console.log(value, index)); // Output: // 1 0 // 2 1 // 3 2

Type Parameters

  • The type of elements in the array.

Parameters

array: ArrayLike<T>
  • The array to iterate over.
optional
callback: (
value: T,
index: number,
array: ArrayLike<T>,
) => unknown
  • The function invoked for each element. The callback function receives three arguments:
  • 'value': The current element being processed in the array.
  • 'index': The index of the current element being processed in the array.
  • 'array': The array 'forEach' was called upon.

Return Type

ArrayLike<T>

Returns the original array.

forEach<T extends
object
| null
| undefined
>
(
object: T,
callback?: (
value: T[keyof T],
key: keyof T,
object: T,
) => unknown
,
): T

Iterates over each element of the object invoking the provided callback function for each property.

Examples

Example 1

forEach({'a': 1, 'b': 2 }, (value, key, object) => console.log(value, key)); // Output: // 1 'a' // 2 'b'

Type Parameters

T extends
object
| null
| undefined
  • The type of object.

Parameters

object: T
  • The object to iterate over.
optional
callback: (
value: T[keyof T],
key: keyof T,
object: T,
) => unknown
  • The function invoked for each property. The callback function receives three arguments:
  • 'value': The current property being processed in the object.
  • 'key': The key of the current property being processed in the object.
  • 'object': The object 'forEach' was called upon.

Return Type

Returns the original object.

Add Package

deno add jsr:@es-toolkit/es-toolkit

Import symbol

import { forEach } from "@es-toolkit/es-toolkit/compat";

---- OR ----

Import directly with a jsr specifier

import { forEach } from "jsr:@es-toolkit/es-toolkit/compat";

Add Package

npx jsr add @es-toolkit/es-toolkit

Import symbol

import { forEach } from "@es-toolkit/es-toolkit/compat";

Add Package

yarn dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { forEach } from "@es-toolkit/es-toolkit/compat";

Add Package

pnpm dlx jsr add @es-toolkit/es-toolkit

Import symbol

import { forEach } from "@es-toolkit/es-toolkit/compat";

Add Package

bunx jsr add @es-toolkit/es-toolkit

Import symbol

import { forEach } from "@es-toolkit/es-toolkit/compat";