array map in typescript

To array map in TypeScript, you can use the map method available on arrays. Here is an example:

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

const doubledNumbers: number[] = numbers.map(num => num * 2);

console.log(doubledNumbers); // Output: [2, 4, 6, 8, 10]
164 chars
6 lines

In this example, we have an array of numbers. We use the map method to iterate through each element in the array and double each number, resulting in a new array doubledNumbers that contains the doubled values of the original array.

Remember that map does not mutate the original array but instead returns a new array with the modified elements.

related categories

gistlibby LogSnag