【问题标题】:Using Map function over an array of objects inside React native not working在 React native 中的对象数组上使用 Map 函数不起作用
【发布时间】:2021-04-29 20:07:02
【问题描述】:

我有一个包含学生详细信息的两个对象的数组。我想使用 map 函数迭代数组,以使用 react native 在屏幕上显示两个学生的姓名和“sub1”。 我的数组是这样的:

const Students = [{'id':"123",
              'name':'Rahul',
              'subjects':{
                'sub1': 'math',
                'sub2':'physics'}
              },
              {'id':"345",
              'name':'Maximilian',
              'subjects':{
                'sub1': 'French',
                'sub2': 'English'}
              }]

我正在编写如下的地图函数:

{Students.map((student:any)=> (
  <View>  
   <Text>
      {student.name},
      {student.subjects.sub1}
   </Text>
  </View>
 ))}

我想查看文本

拉胡尔,数学马克西米利安,法语

在我的屏幕上。我错过了什么?

【问题讨论】:

  • 你需要使用&lt;Text&gt;组件
  • 我编辑了我的问题,添加 后它没有显示任何内容。

标签: react-native ecmascript-6 map-function


【解决方案1】:

试试这个:

return(
//display here whatever you want to 
{Students.map((student:any)=> (
  return(<View>  
   <Text>
      {student.name},
      {student.subjects.sub1}
   </Text>
  </View>)
 ))}
)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2021-09-05
    • 2020-10-05
    • 2020-05-01
    • 1970-01-01
    • 2018-01-16
    • 2018-05-27
    相关资源
    最近更新 更多