【发布时间】:2022-01-03 12:07:28
【问题描述】:
如何正确地从数组而不是 [Object] 中提取数据
api客户端
export interface apiRequest {
locations: LocationOptions[];
}
interface FOLocations {
locationId: number;
}
interface LocationResult {
location: FOLocations[];
cityName: string;
}
export const locationCheck = async (
apiKey: string,
payload: apiRequest
): Promise<LocationResult[]> => {
let response = await axios.post<LocationResult[]>(
`api...`,
payload
);
return response.data;
运行代码
const locationPayload : apiRequest = {
locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
};
const locationResponse = await locationCheck(apiKey,locationPayload);
const locationResponseRes = locationResponse;
console.log(locationResponseRes);
我需要获取值 locationResponseRes.cityName 。 实际输出:
[
{
location: { locationId: 1, address: [Object] },
deliveryOptions: [ [Object] ]
},
{
location: { locationId: 2,},
cityName: [ [Object] ]
}
]
{
"location": {
"locationId": 1,
cityName: [ [Object] ]
}
}
预期输出(我在 Postman 中得到的)
[
{
"location": {
"locationId": 1,
"cityName: "London",
}
},
{
"location": {
"locationId": 2,
"cityName": "New York",
},
},
]
以及如何到达locationResponseRes.cityName?
【问题讨论】:
标签: javascript arrays reactjs typescript