I need assistance with incorporating 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;
}
Afterwards, I am trying to execute an axios
post request:
var results = await axios({
method: "post",
url: `${api}/api/vehicles`,
responseType: "json",
data: {
searchTerm: searchTerm
}
});
The response from this post includes the following structured json
object:
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"
},
]
}
}
I am struggling with mapping this received json
data into an array of my typescript class. My attempt so far has been:
for(let result in results.data.suggestions) {
vehicles.push(result.data:Vehicle);
}
However, it seems to be reading the result
as a string due to the declaration using let result
. Upon inspection via console.log
, I can see that it is displaying the array index.