【问题标题】:How to display data from hash in react native?如何在本机反应中显示来自哈希的数据?
【发布时间】:2019-01-23 01:05:39
【问题描述】:

数据:

{
    “services”:[
        {id: 1, title: ”title 1”},
        {id:3, title:”title 3”},
        {id:4, title:”title 4”}
    ],

    “Blogs”:[
       {id:2 title:”title 2”},
       {id:5, title:”Services”}
    ]
}

我想在屏幕上显示这个数据

services : 
            id: 1, title: ”title 1”
            id:3, title:”title 3”
            id:4, title:”title 4”

Blogs : 
          id:2 title:”title 2”
          id:5, title:”Services”

我写了下面的代码,但我得到了键值,但没有得到该键的数组。

{
    Object.keys(data).forEach(function(key) 
    {
        return (    
            <Text>{key}</Text>     
        )
    })
}

如果有人知道,请提出任何解决方案。提前致谢。

【问题讨论】:

  • services.map((item, i) =&gt; {return ( &lt;Text&gt;{item}&lt;/Text&gt; ) 试试这样

标签: arrays reactjs react-native hashmap


【解决方案1】:

你可以这样渲染它:

{Object.entries(data).map(([key, value]) => (
  <View>
    <Text>{`${key} :`}</Text>
    <View style={styles.shiftLeft}>
      { value.map(({ title, id}) => <Text key={id}>{`id: ${id}, title: "${title}"`}</Text>)}
    </View>
  </View>
))}

Here's 世博会演示。

呈现如下:

如果需要,styles.shiftLeft 很简单:

const styles = StyleSheet.create({
  shiftLeft: {
    marginLeft: 15,
  }
});

【讨论】:

    【解决方案2】:

    使用FlatList 进行数据迭代。您只需将 Data 对象传递到此平面列表中,它就会自动循环。如果您需要任何其他帮助,请告诉我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-02
      • 2020-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-24
      • 1970-01-01
      相关资源
      最近更新 更多