Lodash - findLastIndex 方法



語法

_.findLastIndex(array, [predicate=_.identity], [fromIndex=array.length-1])

此方法類似於 _.findIndex,但它的原理是從 collection 的元素從右到左進行迭代。

引數

  • array (陣列) − 要檢查的陣列。

  • [predicate=_.identity] (函式) − 每次迭代呼叫的函式。

  • [fromIndex=array.length-1] (數字) − 搜尋的起始索引。

輸出

  • (數字) − 返回找到元素的索引,否則返回 -1。

示例

var _ = require('lodash');
var users = [
   { user: 'Sam', active: false },
   { user: 'Ted', active: true },
   { user: 'Julie', active: false }
];

var result = _.findLastIndex(users, function(user) { return !user.active; });
console.log(result);

result = _.findLastIndex(users, ['active', false]);
console.log(result);

tester.js 中儲存上述程式。執行以下命令來執行此程式。

命令

\>node tester.js

輸出

0
0
lodash_array.htm
廣告
© . All rights reserved.