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
- js 对象数组 搜索 ep: find key "a" = value "12" in [{"a":12},{"a":999}] = {index:1,{"a":12}}
- python :数组a[index,:]
- Writing a Key Value Store
- return the index of a 'key' in Python dictionary
- 7.1.12 a是变量,执行(a++)+=a;是否合法
- [Solved]Find a Child in a QTreeView
- Find a Mother Vertex in a Graph
- minimum value in a tree
- 三维数组按key排序 Sorting a 3-dimensional array by a key in PHP
- -a = ~a + 1 = ~(a-1)
- 写一个函数满足:给定一个int list a,满足a[i+1]>=a[i],给定int key,找出list a中第一个大于等于key的元素的index,无满足要求的元素则返回-1.
- a += a -= a*a
- a+=a-=a*a
- a=a++,a=++a
- a = a++ 与 a = ++a
- a += a-= a*a
- DataGridView Index -1 does not have a value 错误
- HyperDex: A Searchable Distributed Key-Value Store
- XHR——XMLHttpRequest对象
- bzoj2463: [中山市选2009]谁能赢呢?
- Makefile显示“make: Nothing to be done for xxxx”的解决方法
- uiautomatorviewer
- 通过python socket远程执行命令,并返回值
- js 对象数组 搜索 ep: find key "a" = value "12" in [{"a":12},{"a":999}] = {index:1,{"a":12}}
- Spring获取request和response
- 网际协议IP(3)之网际控制报文协议ICMP(Internet Control Message Protocol)
- HDOJ--1282 回文数猜想
- Aprior c++ 实现
- mysql(一)
- C++第四次作业7
- CSS背景图片
- java 中 Cannot make a static reference to the non-static 解决方法