2017-12-24 17 views
1
import { 
    SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST, 
    GET_RECENT_CHAT_CONTAT_LIST_REQUEST, 
    GET_RECENT_CHAT_CONTAT_LIST_SUCCESS, 
    GET_RECENT_CHAT_CONTAT_LIST_FAILURE 
} from "../actions/action-types"; 

const INTIAL_STATE = { 
    response: null, 
    error: null, 
    loading: false, 
    searchResults: null, 
}; 

searchChatFromRecentChatContactList = (state, text) => { 
    if(state.response && state.response.length > 0) { 
     const response = [...state.response]; 
     const searchResults = response.filter(item => item.displayName.includes(text)); 
     return searchResults; 
    } 
    return []; 
} 

export default (state = INTIAL_STATE, action) => { 
    switch (action.type) { 
    case GET_RECENT_CHAT_CONTAT_LIST_REQUEST: 
     return { ...state, loading: true, response: null, error: null, }; 
    case GET_RECENT_CHAT_CONTAT_LIST_SUCCESS: 
     return { ...state, response: action.payload, loading: false}; 
    case GET_RECENT_CHAT_CONTAT_LIST_FAILURE: 
     return { ...state, response: null, error: action.payload, loading: false }; 
     case SEARCH_CHAT_FROM_RECENT_CHAT_CONTAT_LIST: 
      return {...state, searchResults: searchChatFromRecentChatContactList(state, action.payload)}; 
    default: 
     return state; 
    } 
}; 

を使用していたとき、私は私のstate.responseに文字列の配列を持っていますが、何らかの理由で私の下の方法は、常に、[]を返して期待される出力を与えていません。Array.prototype.filter()私は私の減速に

state.response = [{displayName: 'someText'}, {displayName: 'someText otherText']; 

入力:

searchChatFromRecentChatContactList(state, 'SomeText') 

出力:

[]; 
+1

はあなたに渡しているテキストの値をチェック返しますsearchChatFromRecentChatContactList関数 – stack26

+0

ありがとう@ stack26 :-) –

答えて

0

私は愚かな間違いをした:(

searchChatFromRecentChatContactList = (state, text) => { 
    if(state.response && state.response.length > 0) { 
     const searchText = text.toLowerCase(); 
     const response = [...state.response]; 
     const searchResults = response.filter(item => { 
      if(item.displayName.includes(searchText)) { 
       return true; 
      } else { 
       return false; 
      } 
     }); 
     return searchResults; 
    } 
    return []; 
} 

text.toLowerCase(); //I should have done this. :-)

1

あなたはまだ破壊が原因でArray.prototype.filterの性質上、ここで必要とされていない、それを改善することができ、それが新たに作成された配列

searchChatFromRecentChatContactList = (state, text) => { 
     const searchText = text.toLowerCase(); 
     return state.response && state.response.length ? 
     state.response.filter(item => item.displayName.includes(searchText)) : []; 
    } 
+0

あなたは正しい感謝です。 –

+0

ようこそ。 –

関連する問題