一、findindex 和 findlastindex1.1 findindex1.2 findlastindex1.3 合并 findindex 和 findlastindex二、sortindex2.1 遍历2.2 二分法三、indexof 和 lastindexof3.1 indexof 的第一版实现3.2 indexof 和 lastindexof 通用第一版3.3 indexof 和 lastindexof 通用第二版参考写在最后(免费学习推荐:javascript视频教程)
一、findindex 和 findlastindex
1.1 findindexfindindex()方法返回数组中满足提供的测试函数的第一个元素的索引。若没有找到对应元素则返回-1。
const array1 = [5, 12, 8, 130, 44];const islargenumber = (element) => element > 13;console.log(array1.findindex(islargenumber));// expected output: 3
实现
array.prototype.newfindindex = function(callback) { const _arr = this; const len = _arr.length; for (let i = 0; i < len; i++) { if (callback(_arr[i], i, _arr)) { return i; } } return -1;};const array1 = [5, 12, 8, 130, 44];const islargenumber = (element) => element > 13;console.log(array1.newfindindex(islargenumber));// 3
1.2 findlastindex同样当我们倒叙查找首个满足条件的方法时,可以这样写:
array.prototype.newfindlastindex = function(callback) { const _arr = this; const len = _arr.length; for (let i = len - 1; i >= 0; i--) { if (callback(_arr[i], i, _arr)) { return i; } } return -1;};const array1 = [5, 12, 8, 130, 44];const islargenumber = (element) => element > 13;console.log(array1.newfindlastindex(islargenumber));// 4
上面的代码,和正序查找很相似,仅仅改变遍历的条件。
1.3 合并 findindex 和 findlastindex大家可以看到,除了循环的条件不同,两个方法几乎一模一样,参考 lodash,我们将两个方法精简一下
/** * @private * @param {array} array the array to inspect. * @param {function} predicate the function invoked per iteration. * @param {boolean} [fromright] 从右向左查找 * @returns {number} 返回第一个符合条件元素的下标或-1 */function basefindindex(array, predicate, fromright) { const { length } = array; let index = fromright ? length : -1; // 确定下标的边界 while (fromright ? index-- : ++index < length) { // index-- 用于倒序边界 // ++index 用于正序的边界 if (predicate(array[index], index, array)) { return index; } } return -1;}
再来看看它的兄弟——underscore 的思路就是利用传参的不同,返回不同的函数。
function createindexfinder(dir) { return function(array, predicate, context) { const { length } = array; var index = dir > 0 ? 0 : length - 1; for (; index >= 0 && index < length; index += dir) { if (predicate.call(context, array[index], index, array)) return index; } return -1; };}var findindex = createindexfinder(1);var findlastindex = createindexfinder(-1);
关于 findindex 我们就告一段落了~,再来看看新的场景和实现吧!
二、sortindex
在一个排好序的数组中找到 value 对应的位置,即保证插入数组后,依然保持有序的状态。
const arr = [1, 3, 5];sortedindex(arr, 0); // 0// 不需要插入arr
那么这个又该如何实现呢?
2.1 遍历遍历大家都能想到,虽然它不一定最优解:
function sortindex(array, value) { for (let i = 0; i < array.length; i++) { if (array[i] > value) { return i; } } return array.length;}
2.2 二分法function sortindex(array, value) { let low = 0, high = array.length; while (low < high) { let mid = math.floor((low + high) / 2); if (array[mid] < value) { low = mid + 1; } else { high = mid; } } return high;}
三、indexof 和 lastindexof
indexof():返回在数组中可以找到一个给定元素的第一个索引,如果不存在则返回-1。从数组的前面向后查找,从 fromindex 处开始。lastindexof() :返回指定元素在数组中的最后一个的索引,如果不存在则返回-1。从数组的后面向前查找,从 fromindex 处开始。3.1 indexof 的第一版实现function indexof(array, value) { for (let i = 0; i < array.length; i++) { if (array[i] === value) { return i; } } return -1;}
emmmm…在看过 findindex 和 lastfindindex 的实现后,indexof 也要整整齐齐的啊~
3.2 indexof 和 lastindexof 通用第一版通过参数来创建不同的查找方法
function createindexof(dir) { return function(array, value) { let index = dir > 0 ? 0 : arr.length - 1; for (; index >= 0 && index < arr.length; index += dir) { if (array[i] === value) { return i; } } return -1; };}
3.3 indexof 和 lastindexof 通用第二版这一次,我们允许指定查找位置,我们来看看 fromindex 的作用:
设定开始查找的位置。如果该索引值大于或等于数组长度,意味着不会在数组里查找,返回 -1。
如果参数中提供的索引值是一个负值,则将其作为数组末尾的一个抵消,即 -1 表示从最后一个元素开始查找,-2 表示从倒数第二个元素开始查找 ,以此类推。
注意:如果参数中提供的索引值是一个负值,仍然从前向后查询数组。如果抵消后的索引值仍小于 0,则整个数组都将会被查询。其默认值为 0。
function createindexof(dir) { return function(array, value, fromindex) { // 设定开始查找的位置。如果该索引值大于或等于数组长度,意味着不会在数组里查找,返回 -1。 let length = array == null ? 0 : array.length; let i = 0; if (!length) return -1; if (fromindex >= length) return -1; if (typeof fromindex === number) { if (dir > 0) { // 正序 // 起始点>=0,沿用起始点,否则起始点为从后向前数fromindex i = fromindex >= 0 ? fromindex : math.max(length + fromindex, 0); } else { // 倒序 // 起始点>=0,沿用起始点,否则起始点为从后向前数fromindex length = fromindex >= 0 ? math.min(fromindex + 1, length) : fromindex + length + 1; } } // 起始下标 for ( fromindex = dir > 0 ? i : length - 1; fromindex >= 0 && fromindex < length; fromindex += dir ) { if (array[fromindex] === item) return fromindex; } return -1; };}
写到这里我们在数组中查找元素就结束了,自己实现的和loadsh或underscore还是有很大区别的,如果大家对上面三节的代码任意有更好的实现,请一定写在留言区哦~
相关免费学习推荐:javascript(视频)
以上就是javascript 专题之九:数组中查找指定元素的详细内容。
