2016-11-23 11 views
0

どうすればいいですか?私は、この減速機を持っているネストされたオブジェクトの配列から要素を削除しますか?

{ 
    ..., 
    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) 
      ] 
     } 
    } 

UPDATE

プレイリストの配列は、このようなプレイリストの多くのオブジェクトが含まれているため、各プレイリストには、無限の歌を持つことができます

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

このようなのは

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

私の質問はどのように1つのプレイリストの1曲を削除できますか?現在のプレイリストのインデックスと、現在のプレイリストの曲のインデックスを送信しています。

+0

を使用することができます - あなたは、より簡単に操作することができます - immutable.jsを使用することを検討してください。この場合は – sodik

答えて

4

splice突然変異あなたがしてはならない配列の状態。何をしたい

sliceconcatの組み合わせです:

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

次のように上記ES6スプレッドの構文を使用して書くことができます。

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

EDIT、考慮しますあなたの最近の質問の更新、あなたの減速機は のように見えるはずですこの:あなたはlodashを使用している場合

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

concat(list.slice(indexOfSong + 1);)、何を意味するのでしょうか?this.state.playlist [action.index] ?,あまりにも速く助けてくれてありがとう –

+0

Typo :)それを今修正しました – Pineda

+0

私は ' dこれを少し拡張して、いくつかのコメントを追加しました。これが助けてくれることを願っています。 – Pineda

1

、あなただけの将来のために_.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

redux' reducers'では突然変異が許可されていません – Thaadikkaaran

関連する問題