es-toolkit/docs/reference/array/last.md
minsuKang 883553b39b
feat(last): Add last (#149)
* add last function

* add last test case

* add last bench mark

* add last docs

* fix ko last docs comment

* add last en.mts, ko.mts

* Update src/array/last.ts

* Update src/array/last.spec.ts

* Update docs/reference/array/last.md

* Update docs/ko/reference/array/last.md

* Update docs/ko/reference/array/last.md

* Update docs/reference/array/last.md

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
2024-07-11 09:05:13 +09:00

47 lines
890 B
Markdown

# last
Returns the last element of an array.
This function takes an array and returns the last element of the array. If the array is empty, the function returns `undefined`.
## Signature
```typescript
function last<T>(arr: [...T[], T]): T;
function last<T>(arr: T[]): T | undefined;
```
### Parameters
- `arr`(`T[]`): The array from which to get the last element.
### Returns
(`T | undefined`): The last element of the array, or `undefined` if the array is empty.
## Example
```typescript
const arr1 = [1, 2, 3];
const result = last(arr1);
// result will be 3
const arr2: number[] = [];
const result = last(arr2);
// result will be undefined
const largeArray = Array(1000)
.fill(0)
.map((_, i) => i);
const result = last(largeArray);
// result will be 999
const nestedArray = [
[3, 1],
[3, 2],
[3, 3],
];
const result = last(nestedArray);
// result will be [3,3]
```