javascript - 映射存储在 Redux 存储中的 JSON 对象?
问题描述
我想映射一个对象,以便在我的 JSX 中可以访问它所说的每个地方的值value
。该对象存储为 const {weather} = this.props
。这是 JSX:
render() {
const { weather } = this.props;
return (
<div className='card border-secondary mb-3'>
<div className='card-header text-white bg-secondary'>City Information</div>
<div className='card-body'>
Lat/Long:
<div className="dropdown-divider"></div>
<div className='row'>
<div className='col-md-4 text-center'>
<h5>Tempurature (F)</h5>
<h6 className ='text-info'>value</h6>
</div>
<div className='col-md-4 text-center'>
<h5>Low (F)</h5>
<h6 className ='text-info'>value</h6>
</div>
<div className='col-md-4 text-center'>
<h5>High (F)</h5>
<h6 className ='text-info'>value</h6>
</div>
</div>
<div className='row'>
<div className='col-md-4 text-center'>
<h5>Pressure</h5>
<h6 className ='text-info'>value</h6>
</div>
<div className='col-md-4 text-center'>
<h5>Humidity</h5>
<h6 className ='text-info'>value</h6>
</div>
<div className='col-md-4 text-center'>
<h5>Wind Speed</h5>
<h6 className ='text-info'>value</h6>
<h5>Lat</h5>
<h6 className ='text-info'>value</h6>
</div>
<h5>Long</h5>
<h6 className ='text-info'>value</h6>
</div>
</div>
</div>
</div>
</div>
);
}
}
我的对象如下所示:
{"coord":{"lon":-95.37,"lat":29.76},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":285.56,"pressure":1022,"humidity":67,"temp_min":283.05,"temp_max":287.15},"visibility":16093,"wind":{"speed":2.27,"deg":115.002},"clouds":{"all":1},"dt":1542935700,"sys":{"type":1,"id":2646,"message":0.0041,"country":"US","sunrise":1542977570,"sunset":1543015369},"id":4699066,"name":"Houston","cod":200}
如何.map
在我的对象上使用方法来呈现我想要显示的值?非常感谢。
解决方案
要映射对象的属性而不是通常的数组,您需要使用该Object.keys(this.props.weather).map(...)
函数。这将为您提供天气对象的键名(即:坐标、风)。然后,您可以使用该名称来获取天气对象的属性,如下所示:this.props.weather[weatherPropertyKey].speed
例如,对于“风”键。
这是一个小演示:
class Weather extends React.Component {
constructor(props) {
super(props);
this.state = {
weather: {"coord":{"lon":-95.37,"lat":29.76},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":285.56,"pressure":1022,"humidity":67,"temp_min":283.05,"temp_max":287.15},"visibility":16093,"wind":{"speed":2.27,"deg":115.002},"clouds":{"all":1},"dt":1542935700,"sys":{"type":1,"id":2646,"message":0.0041,"country":"US","sunrise":1542977570,"sunset":1543015369},"id":4699066,"name":"Houston","cod":200}
};
}
render() {
return (
<div>
{this.state.weather &&
Object.keys(this.state.weather).map((weatherPropertyKey) => {
return <div><b>{weatherPropertyKey}</b> = {JSON.stringify(this.state.weather[weatherPropertyKey])}</div>;
})}
</div>
);
}
}
// Render it
ReactDOM.render(
<Weather/>,
document.getElementById("react")
);
<div id="react"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
推荐阅读
- reactjs - React 如何执行这个特定的代码
- r - 如何使用 set.seed() 函数在 R 中生成随机样本?
- c++ - 在 lambda 函数中保存其返回值时,std::ranges::sort 不能用作“constexpr”函数
- python - 如何在一个时间范围内按多列查找重复行
- swift - macOS Catalina 10.15.6、Xcode 11.6、Mac Catalyst 应用程序中的 PCH 文件和 SwiftShims 错误中的 Objective-C 运行时不同
- python - ValueError:无法将字符串转换为浮点数:使用 matplotlib、arduino 和 pyqt5 时
- javascript - 如何在 Ejs 模板中打印 Web 抓取的项目?
- asp.net - MAC OS 上 Firefox 和 Chrome 的重音字母
- python - Pandas 行列转换
- android - 如何在配套设备配对屏幕中捕捉用户取消?