如何通过 React 按其键返回对象内容(JSON 文件)
how to return the object content (JSON file) by their key through React?
我有一个包含多个停车场信息的 Json 字典文件。 车库名称是该车库详细信息的关键。数据结构是这样的
我正在尝试在 React 中编写一个 for 循环,以列表格式将所有车库信息拉到我的网页。
我的代码:
MyComponents.Garage = React.createClass({
render: function() {
var garage = this.props.garage
console.log(garage)
return (
<li className="card-content">
TODO: This is a component about a garage whose
raw data is //{JSON.stringify(this.props.garage)}
<MyComponents.GarageTitle
title={this.props.garage.friendlyName}/>
<MyComponents.GarageSpaces
open={this.props.garage.open_spaces}
total={this.props.garage.total_spaces}/>
<MyComponents.GarageRates
rates={this.props.garage.rates}/>
<MyComponents.GarageHours
hours={this.props.garage.hours}/>
</li>
);
}
});
MyComponents.Garages = React.createClass({
render: function(){
console.log(this.props.garage)
for (var key in this.props.garage){
console.log(this.props.garage[key])
return (
<ul>
<MyComponents.Garage garage={this.props.garage[key]}/>
</ul>
);
}
}
});
我首先将 Json 数据传递给 Mycomponent.Garages,并在内部运行 for 循环,并调用 Mycomponent.Garage 以获取该 Garage 的每个详细信息。
但我的问题是我只能跑过第一个车库,它不会继续循环剩余的车库。
谁能帮我完成任务?
谢谢
在 for in 循环中使用 return 是行不通的,但你已经接近正确了!相反,请尝试创建车库节点数组:
MyComponents.Garages = React.createClass({
render: function(){
garageNodes = [];
for (var key in this.props.garage){
console.log(this.props.garage[key])
garageNodes.push(
<ul>
<MyComponents.Garage garage={this.props.garage[key]}/>
</ul>
);
}
return garageNodes;
}
});
相关文章:
- Javascript-如何读取json文件中的列并将其保存在Javascript数组中
- 在使用Polymer'加载所有json文件后执行方法;s的核心ajax
- 不显示带有本地json文件数据的谷歌地图脚本
- 可以't使用Appcelerator将JSON文件解析为JavaScript中的TableView
- 加载两个具有相同父密钥名称的json文件
- 使用JSON文件中的变量(字符串)填充文本区域
- 动态显示JSON文件内容
- 正在尝试将JSON文件放入JS数组
- 如何检查Json文件更新,如果更新了,则用更新的数据刷新我的页面
- 单击按钮更改加载到表中的JSON文件
- 处理一个JSON文件;完全相同的副本不是
- 在Javascript中加载JSON文件
- 使用Bootstrap将JSON文件加载到表中
- Angular 2:在本地.json文件上找不到文件
- JSON-从浏览器向外部服务器发送哪些http头JSON文件
- 如何获取d3.js中json文件中具有特定值的总行数
- 如何在dojo应用程序构建概要文件中加载json文件
- jQuery UI使用json文件自动完成
- 通过Azure存储以HTML形式获取JSON文件
- ngResource没有'从JSON文件解析HTML时不起作用