我有一组对象,每个对象都包含有关某个位置的天气信息。当我要访问数组中的spficif对象属性时遇到问题。返回未定义。
ngOnInit() {
console.log(WEATHER_ITEMS);
console.log(WEATHER_ITEMS[0].cityName);
CLASS
import { CurrentWeather } from './current-weather';
export const WEATHER_ITEMS: CurrentWeather[] = [
];
服务
addWeatherItem(weatherItem: CurrentWeather){
WEATHER_ITEMS.push(weatherItem);
}
答案 0 :(得分:-1)
cityName存在是对象CurrentWeather。因此,首先访问CurrentWeather,然后访问cityName
WEATHER_ITEMS[0].CurrentWeather.cityName