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);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值