【发布时间】:2016-11-23 19:22:46
【问题描述】:
我该怎么做?我的店铺是这样的:
{
...,
playlist : [
...,
{
id : 1,
title : "fancy-playlist-title",
songs : [
{ id : 1 },
{ id : 2 },
... and so on
]
}
]
}
我有这个减速器:
if(action.type === "REMOVE_FROM_PLAYLIST"){
return {
...state,
playlist : [
...state.playlist,
...state.playlist[action.index].songs.splice(0, action.indexSongs),
...state.playlist[action.index].songs.splice(action.indexSongs+1)
]
}
}
更新
每个播放列表可以有无限的歌曲,因为播放列表数组包含很多这样的播放列表对象
playlist : [
{
id : 1,
title : "title",
songs : []
},{
id : 2,
title : "playlist 2",
songs : []
},
{... and so on}
]
我的完整reducer是这样的
export default function(state = {}, action) {
if(action.type === "REMOVE_FROM_PLAYLIST"){
//action.index : current index of playlist
//action.indexSongs : current index of song that I want to delete from current playlist
let playlist = state.playlist[action.index].slice(0, action.index).concat(state.playlist[action.index].slice(action.index + 1));
return {
...state,
playlist : [
...state.playlist,
...playlist.slice(0, action.indexSongs),
...playlist.slice(action.indexSongs + 1)
]
}
}
return state;
}
我的问题是如何删除一个播放列表中的一首歌曲?我正在发送当前播放列表的索引和当前播放列表的歌曲索引。
【问题讨论】:
-
只是为了将来 - 考虑使用 immutable.js - 你可以更轻松地操作它们。
标签: javascript reactjs ecmascript-6 redux