Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 1 addition & 56 deletions docs/compat/reference/array/each.md
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
# each (Lodash Compatibility)

::: warning Use `Array.prototype.forEach`

This `each` function operates slowly due to complex type processing and support for various collection types.

Instead, use the faster and more modern `Array.prototype.forEach`.

:::

Performs an iteration operation on each element of an array or object.

```typescript
const result = each(collection, iteratee);
```

## Usage

### `each(collection, iteratee?)`

Iterates through each element of an array, object, or string and executes the given function. For arrays, it iterates in index order; for objects, it iterates through enumerable properties.

```typescript
import { each } from 'es-toolkit/compat';

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

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

// Iterate string
each('hello', (char, index) => console.log(char, index));
// Logs: 'h' 0, 'e' 1, 'l' 2, 'l' 3, 'o' 4
```

If the function returns `false`, iteration stops.

```typescript
import { each } from 'es-toolkit/compat';

each([1, 2, 3, 4], value => {
console.log(value);
return value !== 2; // Stop at 2
});
// Logs: 1, 2
```

#### Parameters

- `collection` (`ArrayLike<T> | Record<any, any> | string | null | undefined`): The collection to iterate over.
- `iteratee` (`(item: any, index: any, collection: any) => unknown`, optional): The function to execute for each element. Default is the `identity` function.

#### Returns

(`ArrayLike<T> | Record<any, any> | string | null | undefined`): Returns the original collection.
`each` is an alias of [`forEach`](./forEach.md). See the [`forEach`](./forEach.md) documentation for details.
Comment on lines -58 to +3

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the detailed documentation from the aliased function.

Using each and forEach as an example, they provide the same functionality, so I think their documentation should remain consistent as well. If both documents contain detailed explanations, any future changes will need to be made in both places.

If one of them is updated without the other being reviewed, we could easily end up in a situation where two aliases provide different documentation for the same behavior.

For that reason, I removed the detailed description from each and had it refer to the forEach documentation instead.

Previously, both functions had detailed documentation. Was there a particular reason for that?

12 changes: 8 additions & 4 deletions docs/compat/reference/array/forEach.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ forEach(collection, callback);

## Usage

### `forEach(collection, callback)`
### `forEach(collection, callback?)`

Use `forEach` when you want to iterate over all elements of an array or object and execute a callback function for each element. The iteration stops if the callback returns `false`.

Expand Down Expand Up @@ -76,11 +76,15 @@ forEach(undefined, value => {
});
```

#### Alias

`each`

Comment on lines +79 to +82

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I also added an Alias section to the forEach documentation to make it clear that each is an alias of forEach.
(Lodash includes a similar section, which I think helps with discoverability as well.)

I'm not sure whether this is the best place for the section. If you think there's a more appropriate location, please let me know!

#### Parameters

- `collection` (`ArrayLike<T> | Record<string, unknown> | null | undefined`): The array or object to iterate over.
- `callback` (`(value: T, index: number | string, collection: any) => void | false`): The function to execute for each element. Returns `false` to stop iteration.
- `collection` (`ArrayLike<T> | Record<any, any> | string | null | undefined`): The array or object to iterate over.
- `callback` (`(item: any, index: any, collection: any) => unknown`, optional): The function to execute for each element. Default is the `identity` function.
Comment on lines +85 to +86

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

noticed that the types documented in each were written correctly, so I applied that to forEach. Going forward, when consolidating docs for other aliases beyond each <-> forEach, what do you think about just taking whichever version is more accurate between the two and consolidating them into one, rather than keeping both?


#### Returns

(`T`): Returns the original collection that was iterated over.
(`ArrayLike<T> | Record<any, any> | string | null | undefined`): Returns the original collection.