react-native - 如何在本机反应中根据其键操作映射项
问题描述
我.map
用来在屏幕上显示我的数组项。数组 ->[Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday]
下面是我的代码:
<ScrollView
horizontal={true}
showsHorizontalScrollIndicator={false}
contentContainerStyle={styles.horizontalView}>
{weekArray.map((item, key) => ((
<Text key={key} style={styles.TextStyle} onPress={_press}>
{" "}{item.day}{" | "}
</Text>
))
)}
</ScrollView>;
我的输出方式如下:
Monday | Tuesday | Wednesday| Thursday| Friday | Saturday | Sunday |
我想删除|
星期天之后的最后一个。我对学习地图很陌生,所以我不知道该怎么做。我知道它对键做了一些事情,但我不知道该怎么做。
解决方案
第二个参数map
是index
... 所以你可以lastItem
用这种方式检查 ...
{weekArray.map((item, key) => (
<Text key={key} style={styles.TextStyle} onPress={_press}>
{key === weekArray.length - 1 ? ` ${item.day}` : ` ${item.day} | `}
</Text>
))}
使用if-cond
...
const renderItem = (index, item) => {
if (index === weekArray.length - 1) {
return ` ${item.day}`;
}
return ` ${item.day} | `;
};
...
{weekArray.map((item, key) => (
<Text key={key} style={styles.TextStyle} onPress={_press}>
{renderItem(key, item)}
</Text>
))}
推荐阅读
- python-3.x - Sort list of current time and date objects in python
- node.js - 多节点实例,NGINX 反向代理,服务页面,发布请求时出现 404 错误?
- powershell - Most efficient way to search event log messages
- reporting-services - 如何使用 SSRS 将报告数据呈现到带有页脚内容的电子邮件正文中
- html - 使用 Vue.js 的按钮位置
- javascript - 我的提交按钮不允许在 html 中点击
- java - Android 10 中的 Environment.getExternalStoragePublicDirectory()
- java - 使用 Stream 解析复杂的用户输入
- android - 用 Dagger 打破 MockK 类验证
- rust - 我可以创建自己的条件编译属性吗?