如何在react中显示对象属性的属性值?
像下面这样
Temperature: {
Metric: {
Value: 27.8,
Unit: "C",
UnitType: 17
},
Imperial: {
Value: 82,
Unit: "F",
UnitType: 18
}
};
我只想写指标值:27.8
我在下面编写了此代码,但出现错误。
import React from "react";
import Cities from "./Cities";
const CityList = ({ cities }) => {
const cityArray = cities.map((user, i) => {
return (
<Cities
key={user[i].Key}
WeatherText={user[i].WeatherText}
EnglishName={user[i].EnglishName}
Value={user[i].Temperature.Metric.Value}
/>
);
});
return (
<div className="tc">
<div> {cityArray} </div>
</div>
);
};
export default CityList;
谢谢
答案 0 :(得分:0)
你可以做
const CityList = ({ cities }) => {
const cityArray = cities.map((city) => {
return (
<Cities
key={city.Key}
WeatherText={city.WeatherText}
EnglishName={city.EnglishName}
Value={city.Temperature.Metric.Value}
/>
);
});
此外,变量不应以大写字母开头。