js 对象数组 搜索 ep: find key "a" = value "12" in [{"a":12},{"a":999}] = {index:1,{"a":12}}

来源:互联网 发布:美萍软件使用教程 编辑:程序博客网 时间:2024/05/21 18:53

思路简介:
对于 对象数组,我们将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);        }
0 0
原创粉丝点击