_.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
相關用法
- Lodash _.min()用法及代碼示例
- Lodash _.take()用法及代碼示例
- Lodash _.xor()用法及代碼示例
- Lodash _.max()用法及代碼示例
- Lodash _.last()用法及代碼示例
- Lodash _.nth()用法及代碼示例
- Lodash _.every()用法及代碼示例
- Lodash _.zip()用法及代碼示例
- Lodash _.add()用法及代碼示例
- Lodash _.indexOf()用法及代碼示例
- Lodash _.startsWith()用法及代碼示例
- Lodash _.pull()用法及代碼示例
- Lodash _.startCase()用法及代碼示例
- Lodash _.xorBy()用法及代碼示例
- Lodash _.fromPairs()用法及代碼示例
- Lodash _.takeRight()用法及代碼示例
- Lodash _.sortedLastIndex()用法及代碼示例
- Lodash _.differenceBy()用法及代碼示例
- Lodash _.isSymbol()用法及代碼示例
- Lodash _.differenceWith()用法及代碼示例
注:本文由純淨天空篩選整理自shivanisinghss2110大神的英文原創作品 Lodash _.sortedLastIndexOf() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。