【问题标题】:Render Nested json objects in React Native在 React Native 中渲染嵌套的 json 对象
【发布时间】:2018-01-14 08:41:26
【问题描述】:

在我的 render() 方法中,我需要解析嵌套的 json 对象。请参阅下面的渲染和 json 结构部分。 我使用 {params.name_last} 访问姓氏。 我将如何访问团队下的项目,例如 team.name_first

render() {
        let { params } = this.props.navigation.state
        <Text>{params.name_last}</Text>
}

[
	{
		"id": 1,
		"name_first": "Name first 1",
		"name_middle": "",
		"name_last": "Name last 1",
		"name_suffix": "",
		"phone": "888-8888",
		"fax": "888-8888",
		"updated_at": "2015-11-02T21:42:42.000Z",
		"team": [
			{
				"id": 16,
				"name_first": "aaa",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			},
			{
				"id": 28,
				"name_first": "aaa",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			},
			{
				"id": 29,
				"name_first": "aaa ",
				"name_middle": "",
				"name_last": "bbb",
				"name_suffix": ""
			}
		]
	}
 ]

【问题讨论】:

    标签: json react-native


    【解决方案1】:

    由于team 是一个数组,您需要访问数组中的特定条目,或者遍历整个事物。

    要到达嵌套数组条目中的特定属性(假设您希望对象位于索引i):

    params.team[i].name_first
    

    创建一个名字数组:

    params.team.map(x => x.name_first)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-06-25
      • 1970-01-01
      • 2020-09-24
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-17
      相关资源
      最近更新 更多