Lodash - nth 方法



語法

_.nth(array, [n=0])

獲取陣列中除最後一個元素之外的所有元素。

引數

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

  • [n=0] (數字) − 要返回的元素的索引。

輸出

  • (*) − 返回 array 的第 n 個元素。

示例

var _ = require('lodash');
var list = [1, 2, 3, 2, 5, 6, 2];

var result = _.nth(list,2)
console.log(result);

var result = _.nth(list,3)
console.log(result);

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

命令

\>node tester.js

輸出

3
2
lodash_array.htm
廣告
© . All rights reserved.