How to convert an array of ints to an array of strings in TypeScript

1 Answer

0 votes
function convertToStringArray(numbers: number[]): string[] {
    // Map each integer to a string
    return numbers.map(num => num.toString());
}

const numbers: number[] = [1, 2, 3, 4, 5];

// Convert the array of integers to a string array
const stringArray: string[] = convertToStringArray(numbers);

console.log("String array:");
stringArray.forEach(str => console.log(str));


   
/*
run:
    
"String array:" 
"1" 
"2" 
"3" 
"4" 
"5" 

*/

 



answered Apr 2, 2025 by avibootz
...