Skip to content

Instantly share code, notes, and snippets.

@ca057
Created February 17, 2022 09:01
Show Gist options
  • Save ca057/86137d44390e96851ba50ad60dd6927e to your computer and use it in GitHub Desktop.
Save ca057/86137d44390e96851ba50ad60dd6927e to your computer and use it in GitHub Desktop.
const padArrayEnd = <T>(arr: T[], targetLength: number, val: T): T[] => {
if (arr.length >= targetLength) return [...arr]
return arr.concat(Array(targetLength - arr.length).fill(val))
}
console.log(padArrayEnd(["0", "1"], 3, ""))
console.log(padArrayEnd(["0", "1", "2"], 3, ""))
console.log(padArrayEnd(["0", "1", "2", "3"], 3, ""))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment