教程 lodash tutorial

_.sortedIndexOf(array, value)

_.sortedIndexOf(array, value) 是 Lodash 库提供的 JavaScript 方法。它用于在已排序的数组中查找指定值的第一个索引。如果找不到指定值,则返回 -1。

它的工作方式如下:

  • array:要在其中查找索引的已排序数组。
  • value:要查找其索引的值。

应用举例:

javascript
const _ = require("lodash");

// 已排序的数组
const nums = [-6, -3, -3, 0, 1, 1, 4, 7];

// 要查找的值
const value = 1;

// 找到值的第一个索引
const index = _.sortedIndexOf(nums, value);

console.log("值的第一个索引:", index);

在这个例子中,我们使用 _.sortedIndexOf 方法找到了值 1 在已排序数组 nums 中的第一个索引位置。