Create an array of Objects and fill it with initial values in Typescript
Obviously, it's an ordinary case, when you get an array of objects and you need to create for the reply the array of other Objects, but you need to initialize it on load. For React developers it can be often used with useEffect or componentDidMount.
For example, you got the array:
const matchesData = await this._apiRep.getMatches();
And you need to create an array of the same size with some values from it. 'Map' will help you.
let defaultForecasts:IMatchForecast[] = matchesData.map(x => {
return (
{
betId: x.id??0,
home: 0, away: 0, userId: 1
}
)
})
As a result, you initialize the array and fill it with required properties.
A Hidden Visual Studio Trick: Execute Selected Code with double Ctrl+E
What do to with Zone.Identifier files after importing a WSL machine and copying data
Support for SharePoint Server 2019 ends on Tuesday, July 14, 2026