I have a typescript
class:
export class Vehicle {
constructor(
id: string,
makeId: string,
makeName: string,
modelName: string,
) {
this.id = id;
this.makeId = makeId;
this.makeName = makeName;
this.modelName = modelName;
}
public id: string;
public makeId: string;
public makeName: string;
public modelName: string;
}
Next, I have an axios
post:
var results = await axios({
method: "post",
url: `${api}/api/vehicles`,
responseType: "json",
data: {
searchTerm: searchTerm
}
});
This post returns the following json
:
results: {
data: {
suggestions: [
{
data: {
id: "93dbae75-cf32-11e9-904a-88d7f67d5c52",
makeId: "8641d37e-cf1e-11e9-904a-88d7f67d5c52",
makeName: "Audi",
modelName: "TT RS Coupe"
},
value: "(2012) Audi - TT RS Coupe"
},
{
data: {
id: "93dcc3f4-cf32-11e9-904a-88d7f67d5c52",
makeId: "8641d37e-cf1e-11e9-904a-88d7f67d5c52",
makeName: "Audi",
modelName: "TT RS Coupe"
},
value: "(2013) Audi - TT RS Coupe"
},
{
data: {
id: "72c4afcb-cf32-11e9-904a-88d7f67d5c52",
makeId: "862fba2f-cf1e-11e9-904a-88d7f67d5c52",
makeName: "Acura",
modelName: "RSX"
},
value: "(2003) Acura - RSX"
},
]
}
}
How do i map this json
resultset into an array of my typescript class? I've tried the following:
for(let result in results.data.suggestions) {
vehicles.push(result.data:Vehicle);
}
It is, however, picking up the result
in let result
as a string. If I console.log
the result, I get the array index.