reactjs - 反应原生从数组列表中删除一个项目
问题描述
我正在尝试从反应本机的列表中删除一个项目,但它不起作用
handleDeletePost = (passedItem) => {
const { userPosts } = this.state;
const newArray = userPosts.map(item => {
if (item.headline === passedItem.headline) {
Alert.alert(
'Delete Post',
'Are you sure to delete Post?',
[
{text: 'Yes', onPress: () => console.log('Ask me later pressed')},
{text: 'No', onPress: () => console.log('Cancel Pressed'), style: 'cancel'}
],
{ cancelable: false }
)
}
});
}
当我点击删除时出现错误:找不到变量索引
解决方案
您的地图看起来没有正确使用,但要从数组中删除项目,请使用 .splice() 函数。
例子
var array = [2, 5, 9];
console.log(array)
var index = array.indexOf(5);
if (index > -1) {
array.splice(index, 1);
}
输出
array[2, 9]
希望这可以帮助 :)
推荐阅读
- python - reduce() 和 Reduce()(大写和小写)
- unicode - 无法在文件标题上插入 Unicode 字符
- android - 如何在 MVP Android 中向 Model 类注入上下文?
- python - 有断开按钮和连接按钮?
- docker - 为什么 Ubuntu docker 映像不是 VM
- javascript - 无法使用 admin sdk 获取 Firestore 文档
- php - PHP MySQL - 在数据库中的两个单独的表上获取相同的 ID
- php - 如何在下拉列表中显示当前年份和明年类似 2018-2019 的内容?
- java - 本机查询问题
- python - 从文本文件中提取信息并使用 python 生成输出 csv 表