stream,作減法,優化搜索代碼。

作一個搜索,三個輸入條件,求這個條件的交集。起初個人思路是按照操做的流程,一步步的來作這三個篩選。javascript

let searchResults = [];
//step1 根據id搜索,獲得一個子集。
if (searchId) {
    //開始按照id查找,查找目標爲allData
    let eds = allData;
    searchResults = this._searchWithId(searchId, eds);
    searchFlag = true;
}

if (searchTime) {
    //開始按照searchtime查找,若是searchFlag爲true則查找目標爲searchResults,不然爲allData
    searchTime = Number(searchTime);
    if (searchFlag === true) {
        let srs = [];
        srs= this._searchWithMilliSeconds(searchId, searchResults );
        searchResults = srs;
    } else {
        let eds = allData;
        searchResults = this._searchWithMilliSeconds(searchId, eds);
    }

    searchFlag = true;
}

if (searchWords) {
    //開始按照searchwords查找,若是searchFlag爲true則查找目標爲searchResults,不然爲allData
    if (searchFlag === true) {
        let srs = [];
        srs = this._searchWithKeyWord(searchId, searchResults );

        searchResults = srs;
    } else {
        let eds = allData;
        searchResults = this._searchWithKeyWord(searchId, eds);
    }

    searchFlag = true;
}
let result = searchDataStream;

通過某同事的幫助,採用作減法的思想,數據就像是流同樣被filter掉。代碼就被縮減了好多。主要是思路和經驗,你們看看,細節請忽略java

let searchDataStream = allData;
if (searchId) {
    //開始按照id查找,查找目標爲_editLineDatas
    searchDataStream = this._searchWithId(searchId, searchDataStream);
}

if (searchTime) {
    //開始按照searchtime查找,若是searchFlag爲true則查找目標爲searchResults,不然爲_editLineDatas
    searchTime = Number(searchTime);
    searchDataStream = this._searchWithMilliSeconds(searchTime, searchDataStream);
}

if (searchWords) {
    //開始按照searchwords查找,若是searchFlag爲true則查找目標爲searchResults,不然爲_editLineDatas
    searchDataStream = this._searchWithKeyWord(searchWords, searchDataStream);
}
let result = searchDataStream;
相關文章
相關標籤/搜索