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;
相关文章
相关标签/搜索