js 對象數組 搜索 ep: find key "a" = value "12" in [{"a":12},{"a":999}] = {index:1,{"a":12}}

思路簡介:
對於 對象數組,我們將key作爲標準, 按照我們傳入的或默認的 比較器和要比較的某個值傳入方法。
看過java的collection中的sort方法,寫得不錯,這裏借鑑了幾點,
昨天寫的有問題。。。今天改了改,一共5個小方法,compareDefault是默認的比較器,defaultSort是默認的排序方法,
binarySearch是默認的查找方法 這樣就可以擴展 將新的更強大的查找方法放進去了,最後時候兩個主體:findItemInEleArray 和findItemInEleArrayComplex只有complex融合了前方的各種高科技,如果簡單的話就用前面的就好了,當然保證有序(你可以先defaultSort一下)而且返回結果在原數組位置無所謂的話,就可以考慮用第2種

        function compareDefault(prop) {
            return function (obj1, obj2) {
                var val1 = obj1[prop];
                var val2 = obj2[prop];
                if (val1 < val2) {
                    return -1;
                } else if (val1 > val2) {
                    return 1;
                } else {
                    return 0;
                }            
            } 
        }

        function defaultSort( source, key, userDefinedCompare){
            var tempKey = angular.copy(key);
            var tempSource = angular.copy(source);
            // tempSource.sort(compareDefault(tempKey));//$filter('orderBy')(tempSource, tempKey);
            return tempSource.sort((userDefinedCompare)?userDefinedCompare(tempKey):compareDefault(tempKey));
        }

        function binarySearch (source, key, value) {
            for (var left = 0, right = source.length -1 ;left <= right;) {
                var middle = parseInt((parseInt(left) + parseInt(right)) / 2)
                if (source[middle][key] == value){
                    return {
                                "index": middle,
                                "element":source[middle]
                            };
                }else if(source[middle][key] > value) {
                    right = middle - 1;
                } else {
                    left = middle + 1;
                }
            }
            return -1;
        }

        /*
        *簡單的json 狀態 檢查
        */
        function isJsonStringState(item){
            return item && (typeof item == 'string') && item.indexOf('{') > -1;
        }

        /*數組查找 方法
         *params source,key,value
         * return index & element--在比較過成中 我沒有 去掉輸入兩邊的空格--可以加
         */
        function findItemInEleArray(source, key, value) {
            var tempSource = angular.copy(source);
            var tempKey = angular.copy(key);
            var tempValue = angular.copy(value);
            if(tempKey.indexOf('.') == -1){
                tempKey = [tempKey];
            }else{
                tempKey = tempKey.split('.');
            }
            for(var i = 0; i < tempSource.length ; i++){
                var pathFinal = tempSource[i];
                for(var j = 0; j < tempKey.length; j++){
                    if(isJsonStringState(pathFinal)){
                        pathFinal = JSON.parse(pathFinal);
                    }
                    pathFinal = pathFinal[tempKey[j]];
                }
                if(pathFinal == tempValue){
                    return {
                        index : i,
                        element: tempSource[i]
                    }
                }
            }
            return -1;
        }

        function findItemInEleArrayComplex(source, key, value, searchFun) {
            if(source.length < 100){
                return findItemInEleArray(source, key, value);
            }
            var tempSource = angular.copy(source);
            var tempKey = angular.copy(key);
            var tempValue = angular.copy(value);
            //簡單折半查找
            return (searchFun)?searchFun(tempSource, tempKey, tempValue):binarySearch(tempSource, tempKey, tempValue);
        }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章