mirror of
https://github.com/toss/es-toolkit.git
synced 2024-11-27 14:57:44 +03:00
883553b39b
* 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>
890 B
890 B
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
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
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]