How can I do this? My store is like this:
{
...,
playlist : [
...,
{
id : 1,
title : "fancy-playlist-title",
songs : [
{ id : 1 },
{ id : 2 },
... and so on
]
}
]
}
I have this reducer:
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)
]
}
}
UPDATE
each playlist can have infinite songs, because playlist array contains much objects of playlist like this
playlist : [
{
id : 1,
title : "title",
songs : []
},{
id : 2,
title : "playlist 2",
songs : []
},
{... and so on}
]
My complete reducer is like this
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;
}
My question is how can I delete one song of one playlist? I'm sending the index of current playlist and the index of the song of current playlist.