如何使用react访问JSON格式中包含的对象数组中的特定元素



我得到了一个json文件(data.json(,其中包含一个数组中的多个对象,我想获取每个对象中某个键的值。我使用react redux获取这些值,然后将它们显示为网页上的表格。

以下文件是文件的简化版本,在每个数组元素中都有更多的键。这就是为什么我不确定是否使用"state={…",因为我必须列出太多的元素。

{"state":"OK","display":"success","information":
[
{"name":"North","type":"REGION"},
{"name":"South","type":"REGION"},
....

想要的输出将出现在这样的网页上:

North南方。。。…

据我所知,您希望使用name属性创建一个表。这里有一个简单的例子。

class App extends React.Component {
state = {
"state": "OK",
"display": "success",
"information": [
{ "name": "North", "type": "REGION" },
{ "name": "South", "type": "REGION" },
],
};
render() {
return (
<div>
<table>
<tr>
{
this.state.information.map( el => 
<td>{el.name}</td>
)
}
</tr>
</table>
</div>
);
}
}

ReactDOM.render(
<App />,
document.getElementById("app")
);
<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>
<div id="app"></div>

最新更新