2017-10-24 3 views
0

の配列から項目を削除します。私は、この初期状態を持っている:私はReduxのを学んだし、私は状態から一つの項目を削除する方法を思っていたReduxの

export const getInitialState =() => { 
    let state = { 
    isLogged: false, 
    organizations: [], 
    userData: {}, 
    activeIndex: -1, 
    currentRetrospective: {}, 
    hasFetched: false 
    } 

これは、データが今organizations

case `${actions.ACTION_GET_USER_ORGS}_FULFILLED`: { 
    let activeIndex = 0 
    if (state.activeIndex !== -1) { 
     activeIndex = state.activeIndex 
    } else if (action.payload.data.length === 0) { 
     activeIndex = -1 
    } 
    return { ...state, activeIndex, organizations: action.payload.data, hasFetched: true } 
    } 

の内側に住んでいる方法です、私は何をする必要があるかorganizationretrospectives配列から一つの項目を削除しています。私はこれを試しましたが、うまくいきません。それを行うより良い方法はありますか?

export default (state = getInitialState(), action) => { 
    switch (action.type) { 

    case `${actions.ACTION_DELETE_RETROSPECTIVE}_FULFILLED`: { 

    const { organizations, activeIndex } = state 

    const newOrganizations = JSON.parse(JSON.stringify(organizations)) 

    const activeOrganization = newOrganizations[activeIndex] 

    activeOrganization.retrospectives = activeOrganization.retrospectives 
     .filter((retro) => retro.id != action.retroId) 

    return { ...state, organizations: newOrganizations } 
    } 

ありがとうございます!

+0

データはあなたの組織配列の内側に住んでどのように?。それでも –

+0

おかげで、あなたがチェックしているあなたが試験したときに 'action.retroId'は、正しい値が含まれている場合この? –

+0

なぜこの? 'JSON.parse(JSON.stringify(団体))' – bitstrider

答えて

1

あなたは、このような組織の配列フィルタできます

export default (state = getInitialState(), action) => { 
     switch (action.type) { 
      case `${actions.ACTION_DELETE_RETROSPECTIVE}_FULFILLED`: { 
       return { 
        ...state, 
        organizations: state.organization.filter(retro => 
         retro.id !== action.retroId } 
     } 
関連する問題