Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 按索引从redux数组中删除项_Arrays_Reactjs_Redux - Fatal编程技术网

Arrays 按索引从redux数组中删除项

Arrays 按索引从redux数组中删除项,arrays,reactjs,redux,Arrays,Reactjs,Redux,我的react redux存储中存储了一个数组。这些项目包括一个标题,仅此而已(无id字段本身) 当我启动一个操作时,我会尝试使用 case REMOVE_NOTIFICATION: return { ...state, notifications: state.notifications.filter(id => action.id !== id) } action.id是正确的,但是调用它似乎没有任何作用。我希望它将返回一个新的项数组

我的react redux存储中存储了一个数组。这些项目包括一个
标题
,仅此而已(无
id
字段本身)

当我启动一个操作时,我会尝试使用

case REMOVE_NOTIFICATION: 
    return {
        ...state,
        notifications: state.notifications.filter(id => action.id !== id)
    }

action.id
是正确的,但是调用它似乎没有任何作用。我希望它将返回一个新的项数组,其中action.id与传入的项的id不匹配。假设
id
实际上是数组项的索引,对吗

过滤函数中的否第一个参数始终是值,第二个参数是索引/键

试试这个:)


过滤器函数中无第一个参数始终为值,第二个参数为索引/键

试试这个:)

假设id实际上是数组项的索引,对吗

否。因此,id参数将是数组项(内容),而不是它们的索引

您可以通过filter函数的第二个参数访问项目的索引:

case REMOVE_NOTIFICATION: 
return {
    ...state,
    notifications: state.notifications.filter((item, index) => action.id !== index)
}
假设id实际上是数组项的索引,对吗

否。因此,id参数将是数组项(内容),而不是它们的索引

您可以通过filter函数的第二个参数访问项目的索引:

case REMOVE_NOTIFICATION: 
return {
    ...state,
    notifications: state.notifications.filter((item, index) => action.id !== index)
}
假设id实际上是数组项的索引,对吗?不,id将不是项索引,如果要使用该索引,请按如下方式编写:
state.notifications.filter((id,index)=>action.id!==index)
假设id实际上是数组项的索引是否正确?不,id将不是项目索引,如果要使用索引,请按如下方式编写:
state.notifications.filter((id,index)=>action.id!==index)