I want to dynamically create a Typescript Map<string, Person> object where Person is a class.
I can use for loop to create a map like this:
function getInitializedPersonMap(size){
const personMap = new Map<string, Person>();
for (let index = 0; index < size; index++) {
personMap.set(index.toString(), {} as Person);
}
return personMap;
}
However, I am trying to build a habit of moving away from 'for' loops and use functions like map, filter, reduce.
My attempt without loop looks like this which is not quite as readable as one with for loop:
function getInitializedPersonMap(size){
return new Map<string, Person>(
Array.from(new Array(size), (_, index) => [
index.toString(),
{} as Person,
])
);
}
For scenarios like this is this an overkill to avoid 'for' loops?