此代码运行正常。但对我来说看起来很奇怪。特别是这部分`城市常量= [].concat(...locationCities).includes('London');if( === true)
apiClient:
export interface apiRequest {
locations: LocationOptions[];
}
interface FOLocations {
locationId: number;
}
interface LocationResult {
location: FOLocations[];
address: Address;
}
interface LocationResult {
country:string;
city:string;
}
export const locationCheck = async (
apiKey: string,
payload: apiRequest
): Promise<LocationResult[]> => {
let response = await axios.post<LocationResult[]>(
`api...`,
payload
);
return response.data;runCode:
const locationPayload : apiRequest = {
locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
};
const locationResponse = await locationCheck(apiKey,locationPayload);
const locationCities = locationResponse.map((location) => location.address.map(options => {return options.country}));
console.log(locationCities);
const city = [].concat(...locationCities).includes('London');
console.log(city); if(city === true){
console.log(`London location exist`);
}else {
throw new Error(
"error"
);
}输出:
true
London location exist发布于 2021-11-25 20:52:50
我认为你可以删除这一行:
const city = [].concat(...locationCities).includes('London');并替换为:
if(city === true){
// ...有了这个:
if(locationCities.includes('London')){
// ...https://stackoverflow.com/questions/70116734
复制相似问题