2016-11-23 2 views
0

Wie kann ich das tun? Mein Speicher ist wie folgt aus:Element aus dem Array im verschachtelten Objekt löschen?

{ 
    ..., 
    playlist : [ 
     ..., 
     { 
      id : 1, 
      title : "fancy-playlist-title", 
      songs : [ 
      { id : 1 }, 
      { id : 2 }, 
      ... and so on 
      ] 
     } 
    ] 
} 

Ich habe diesen Minderer:

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

jede Playlist unendlich Songs haben kann, denn Playlist Array viele Objekte der Wiedergabeliste enthält wie diese

playlist : [ 
    { 
     id : 1, 
     title : "title", 
     songs : [] 
    },{ 
    id : 2, 
    title : "playlist 2", 
    songs : [] 
    }, 
    {... and so on} 
] 

Meine komplette rot ucer ist so

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; 
} 

Meine Frage ist, wie kann ich einen Song einer Playlist löschen? Ich sende den Index der aktuellen Playlist und den Index des Songs der aktuellen Playlist.

+0

nur für die zukünftige Verwendung - betrachten immutable.js mit - man kann sie leichter manipulieren. – sodik

Antwort

4

splicemutiert der Array-Zustand, den Sie nicht tun sollten.

Was Sie wollen, ist eine Kombination aus slice und concat:

playlist.slice(0, indexOfSong) // copy a portion of the array 
           // from the start to the indexOfSong 

     .concat(    // concatenate it with: 

      playlist.slice(indexOfSong + 1) 
           // copy of a portion of the array from 
           // the index just after indexOfSong 
           // to the end of the playlist 
     ); 

Das Obige kann wie folgt mit ES6 Spread-Syntax geschrieben werden:

[ 
    ...playlist.slice(0, indexOfSong) 
    ...playlist.slice(indexOfSong + 1)); 
] 

EDIT, wenn man bedenkt Ihre letzte Frage aktualisieren, sollte Ihr Reducer wie aussehen dies:

export default function(state = {}, action) { 

if(action.type === "REMOVE_FROM_PLAYLIST"){ 
    return { 
      ...state, 
      playlist : [ 
       ...state.playlist, 
       ...playlist[action.index].songs.slice(0, action.indexSongs), 
       ...playlist[action.index].songs.slice(action.indexSongs + 1) 
      ] 
     } 
    } 
} 
+0

in diesem Fall "concat (list.slice (indexOfSong + 1);", was Liste bedeutet? This.state.playlist [action.index]?, Danke für zu schnell helfen –

+0

Typo :) Korrigiert es jetzt – Pineda

+0

Dachte ich ' d erweitern auf das ein bisschen, so fügte einige Kommentare hinzu, hoffe das hilft – Pineda

1

Wenn Sie lodash verwenden, könnten Sie _.merge

https://lodash.com/docs/4.17.2#merge

if(action.type === "REMOVE_FROM_PLAYLIST"){ 
    let newPlaylistWihoutDeletedItem = state.playlist.splice(action.index, 1); 

    return _.merge({}, state, { 
     playlist: newPlaylistWihoutDeletedItem 
    }); 
} 
+0

Mutation ist nicht erlaubt in reduced 'reducers' – Thaadikkaaran

Verwandte Themen