_.sortedLastIndexBy(array, value, [iteratee=_.identity])

40

_.sortedLastIndexBy(array, value, [iteratee=_.identity])
_.sortedLastIndexBy类似sortedLastIndex,用二分查找来查找到一个最高位的值应该在哪个索引插入数组后还能够保持数组的排序,区别是多提供一个遍历器处理数组每个元素和value
参数

array (Array): 需要查找元素索引的数组
value (*): 要查找的元素

[iteratee=_.identity] (Function): 处理元素的遍历器

返回值

(number): 返回应该插入元素的索引

例子

var objects = [{ 'x': 4 }, { 'x': 5 }];
 
_.sortedLastIndexBy(objects, { 'x': 4 }, function(o) { return o.x; });
// => 1
 
// The `_.property` iteratee shorthand.
_.sortedLastIndexBy(objects, { 'x': 4 }, 'x');
// => 1

源代码:

调用了baseSortedIndexBy,前面看过了,这里省略。

iteratee会被baseIteratee处理,可以处理对象的key的参数,和differenceBy一样,这里省略

import baseSortedIndexBy from './.internal/baseSortedIndexBy.js'

/**
 * This method is like `sortedLastIndex` except that it accepts `iteratee`
 * which is invoked for `value` and each element of `array` to compute their
 * sort ranking. The iteratee is invoked with one argument: (value).
 *
 * @since 4.0.0
 * @category Array
 * @param {Array} array The sorted array to inspect.
 * @param {*} value The value to evaluate.
 * @param {Function} iteratee The iteratee invoked per element.
 * @returns {number} Returns the index at which `value` should be inserted
 *  into `array`.
 * @example
 *
 * const objects = [{ 'n': 4 }, { 'n': 5 }]
 *
 * sortedLastIndexBy(objects, { 'n': 4 }, ({ n }) => n)
 * // => 1
 */
//类似sortedLastIndex,用二分查找来查找到一个最高位的值应该在哪个索引插入数组后还能够保持数组的排序,区别是多提供一个遍历器处理数组每个元素和value
function sortedLastIndexBy(array, value, iteratee) {
  return baseSortedIndexBy(array, value, iteratee, true)
}

export default sortedLastIndexBy

 

posted @ 2018-10-19 11:59  hahazexia  阅读(195)  评论(0)    收藏  举报