es-toolkit/docs/reference/array/toFilled.md
원동휘 ea66835ffb
feat(toFilled): Add toFilled (#154)
* feat: add toFilled

* feat: add test for toFilled

* feat: add bench for toFilled

* docs: add docs for toFilled

* chore: Modified benchmark wording

* Update benchmarks/toFilled.bench.ts

Co-authored-by: jgjgill <79239852+jgjgill@users.noreply.github.com>

* fix: toFilled benchmark

* Apply suggestions from code review

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

* Update docs/reference/array/toFilled.md

---------

Co-authored-by: Sojin Park <raon0211@gmail.com>
Co-authored-by: jgjgill <79239852+jgjgill@users.noreply.github.com>
2024-07-11 10:55:29 +09:00

1.5 KiB

toFilled

Creates a new array filled with a specified value from the start position up to, but not including, the end position.

If the start or end indices are not provided, it defaults to filling the entire array.

Negative indices can also be used, in which case they are counted from the end of the array.

Interface

function toFilled<T, U>(arr: T[], value: U): Array<T | U>;
function toFilled<T, U>(arr: T[], value: U, start: number): Array<T | U>;
function toFilled<T, U>(arr: T[], value: U, start: number, end: number): Array<T | U>;

Parameters

  • arr (Array<T>): The array to base the new array on.
  • value (U): The value to fill the new array with.
  • start (number, default = 0): The start position. Defaults to 0.
  • end (number, default = array.length): The end position. Defaults to the array's length.

Return Value

(Array<T | U>): A new array with the specified values filled in.

Examples

const array = [1, 2, 3, 4, 5];

let result = toFilled(array, '*', 2);
console.log(result); // [1, 2, '*', '*', '*']
console.log(array); // [1, 2, 3, 4, 5]

result = toFilled(array, '*', 1, 4);
console.log(result); // [1, '*', '*', '*', 5]
console.log(array); // [1, 2, 3, 4, 5]

result = toFilled(array, '*');
console.log(result); // ['*', '*', '*', '*', '*']
console.log(array); // [1, 2, 3, 4, 5]

result = toFilled(array, '*', -4, -1);
console.log(result); // [1, '*', '*', '*', 5]
console.log(array); // [1, 2, 3, 4, 5]