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
中有一系列字符串,但出于某种原因,我的下方方法总是返回[];
state.response = [{displayName: 'someText'}, {displayName: 'someText otherText'];
输入
searchChatFromRecentChatContactList(state, 'SomeText')
输出
[];
答案 0 :(得分: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)) : [];
}
答案 1 :(得分: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. :-)