Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Reactjs 在reducer中使用时,Array.prototype.filter()未提供预期的输出_Reactjs_React Native_Ecmascript 6_Redux - Fatal编程技术网

Reactjs 在reducer中使用时,Array.prototype.filter()未提供预期的输出

Reactjs 在reducer中使用时,Array.prototype.filter()未提供预期的输出,reactjs,react-native,ecmascript-6,redux,Reactjs,React Native,Ecmascript 6,Redux,我的状态中有一个字符串数组。response,但由于某种原因,我下面的方法总是返回[] 输入: state.response = [{displayName: 'someText'}, {displayName: 'someText otherText']; 输出: searchChatFromRecentChatContactList(state, 'SomeText') 我犯了一个愚蠢的错误:( text.toLowerCase();//我应该这样做。:-您仍然可以改进它,由于Array

我的
状态中有一个字符串数组。response
,但由于某种原因,我下面的方法总是返回[]

输入:

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

searchChatFromRecentChatContactList(state, 'SomeText')
我犯了一个愚蠢的错误:(


text.toLowerCase();//我应该这样做。:-

您仍然可以改进它,由于Array.prototype.filter的性质,这里不需要销毁,它返回新创建的数组

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

检查要传递给searchChatFromRecentChatContactList函数的文本值谢谢@stack26:-)
[];
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 [];
}
    searchChatFromRecentChatContactList = (state, text) => {
        const searchText = text.toLowerCase();
        return state.response && state.response.length ?
         state.response.filter(item => item.displayName.includes(searchText)) : [];
   }