2017-08-25 31 views
1

我有一個我想根據ID過濾的註釋數組,並更改特定的鍵/值對。我的控制檯日誌函數返回數組中的正確項目,但我不確定使用.filter函數的結果並將'喜歡'鍵從'false'更改爲'true'的正確語法。如果適合這種情況,我可以使用擴展運算符。Redux Reducer - 基於ID的過濾器數組,然後更改鍵/值對

import * as types from '../actions/actionTypes'; 
import initialState from './initialState'; 

export default function commentReducer(state = initialState.comments, action) { 
    switch (action.type) { 
    case types.ADD_COMMENT_LIKE_SUCCESS: 
     const content = Object.assign({}, state); 
     const likedComment = content.data.filter(comment => (
     comment.id === action.commentId 
    )); 
     console.log(likedComment[0]); 
     break; 

    default: 
     return state; 
    } 
} 

的評論對象是這樣的:

"data":{ 
     "data":[ 
     { id: '', comment: '', liked: false }, 
     { id: '', comment: '', liked: false }, 
     { id: '', comment: '', liked: false }, 
     { id: '', comment: '', liked: false } 
     ], 
     "cursor":"CkUKEQoEZGF0ZRIJCNrwhcWhvdUCEixqFGRldn5kZXZlbG9wbWVudC0xMzAwchQLEgdDb21tZW50GICAgLSe_-cKDBgAIAE=", 
     "more":false, 
     "count":4 
    }, 
+0

你的'initialState'是怎麼樣的? – JoseAPL

+0

編輯我的問題,包括它。 – GuerillaRadio

回答

0

您使用的是有正確的語法的過濾功能,雖然你的減速機應該與喜歡的鍵值對,尤其是評論返回所有的評論如此,您可以如何繼續使用使用地圖功能相同的

import * as types from '../actions/actionTypes'; 
import initialState from './initialState'; 

export default function commentReducer(state = initialState.comments, action) { 
    switch (action.type) { 
    case types.ADD_COMMENT_LIKE_SUCCESS: 
     const content = Object.assign({}, state); 
     content.data = content.data.map(comment => { 
         const newObj = {...comment}; 
         if(newObj.id === action.commentId) 
         { 
          newObj.likeKey = true; 
         } 
         return newObj; 
     }); 
    console.log(content); 
    return content; 
    default: 
     return state; 
    } 
} 

地圖functin將返回您到目前爲止的所有評論,並更改該特定評論對象。希望這可以幫助。

+0

這改變了鍵值對,但不幸的是它只返回註釋數組而不是全註釋對象(因此刪除'遊標','更多'和'計數'鍵)。 – GuerillaRadio

+0

我已經更新了anwser中的代碼,我想現在它會返回完整的對象,是嗎? – mindaJalaj

0

我會使用find而不是過濾器,因爲它總是隻返回一個項目。

我不能完全肯定你如何分裂的狀態,但我可以建議返回,如:

import * as types from '../actions/actionTypes'; 
import initialState from './initialState'; 

export default function commentReducer(state = initialState.comments, action) { 
    switch (action.type) { 
    case types.ADD_COMMENT_LIKE_SUCCESS: 
     const otherComments = state.data; 
     const likedComment = state.data.find(comment => (
     comment.id === action.commentId 
    )); 
     const index = state.data.indexOf(likedComment); 

     return { 
      ...state, 
      data: { 
       [ 
        ...otherComments.slice(0, index), 
        { ...likedComment, liked: true }, //Override properties here 
        ...otherComments.slice(index + 1) 
       ] 
      } 
     }; 

    default: 
     return state; 
    } 
} 

沒有必要從國家複製的內容,因爲我不修改狀態。

基本上我:

  1. 複製state和壓倒一切的data鍵與...的所有評論
  2. 一個副本和我副本likedComment串聯,但與被覆蓋的數據
+0

這確實改變了我的鍵值,但是當喜歡評論時,它會將該評論添加到數組的末尾(例如,如果我喜歡4個評論中的第2個,我的Redux狀態會發生變化,以便第2個評論現在處於第4個位置) – GuerillaRadio

+0

@GuerillaRadio你不能添加一個Index或者CreatedAt屬性,所以我們可以對它進行排序嗎? – Latrova

+0

@GuerillaRadio只要想得更好......我們可以通過切片來獲得索引並保存它。編輯我的代碼,試試。 – Latrova