mirror of
https://github.com/freeCodeCamp/freeCodeCamp.git
synced 2025-12-19 18:18:27 -05:00
9 lines
246 B
TypeScript
9 lines
246 B
TypeScript
export function insertInto<T>(arr: T[], index: number, elem: T): T[] {
|
|
if (index >= arr.length) return [...arr, elem];
|
|
if (index <= 0) return [elem, ...arr];
|
|
|
|
return arr.flatMap((x, id) => {
|
|
return id === index ? [elem, x] : x;
|
|
});
|
|
}
|