當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Lodash _.sortedLastIndexOf()用法及代碼示例

_.sortedLastIndexOf方法用於返回可以插入元素的數組的最高索引,並保持其排序順序。此方法也類似於_.lastIndexOf,不同之處在於它對已排序的數組執行二進製搜索。
用法:

_.sortedLastIndexOf(array, value)

參數:該方法接受上述和以下所述的兩個參數:

  • array:此參數保存要檢查的數組。
  • value:此參數保存要搜索的值。

返回值:它返回匹配值的索引,否則為-1。
範例1:在這裏,const _ = require(‘lodash’)用於將lodash庫導入文件中。

javascript

// Requiring the lodash library   
const _ = require("lodash");   
      
// Original array   
let y = [4, 5, 7, 7, 7]    
      
// Use of _.sortedLastIndexOf()   
// method   
let index = _.sortedLastIndexOf(y, 7);  
      
// Printing the output   
console.log(index);

輸出:



4

範例2:

javascript

// Requiring the lodash library  
const _ = require("lodash");  
      
// Original array  
let y = ['p', 'q', 'r', 't', 't', 'u', 's', 't' ]  
      
// Use of _.sortedLastIndexOf()  
// method  
let index = _.sortedLastIndexOf(y, 't');  
      
// Printing the output  
console.log(index);

輸出:

7

相關用法


注:本文由純淨天空篩選整理自shivanisinghss2110大神的英文原創作品 Lodash _.sortedLastIndexOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。