Lodash是一個一致性、模塊化、高性能的 JavaScript 實用工具庫,
Lodash 通過降低 array、number、objects、string 等等的使用難度從而讓 JavaScript 變得更簡單,Lodash 的模塊化方法 非常適用于:
- 遍歷 array、object 和 string
- 對值進行操作和檢測
- 創建符合功能的函式
import lodash from 'lodash';
1、Array方法
1.1 _.findIndex
回傳值(number): 回傳找到元素的 索引值(index),否則回傳 -1,
var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true } ]; _.findIndex(users, function(o) { return o.user == 'barney'; }); // => 0 // The `_.matches` iteratee shorthand. _.findIndex(users, { 'user': 'fred', 'active': false }); // => 1 // The `_.matchesProperty` iteratee shorthand. _.findIndex(users, ['active', false]); // => 0 // The `_.property` iteratee shorthand. _.findIndex(users, 'active'); // => 2
1.2、_.findLastIndex
回傳值(number): 回傳找到元素的 索引值(index),否則回傳 -1,
var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false } ]; _.findLastIndex(users, function(o) { return o.user == 'pebbles'; }); // => 2 // The `_.matches` iteratee shorthand. _.findLastIndex(users, { 'user': 'barney', 'active': true }); // => 0 // The `_.matchesProperty` iteratee shorthand. _.findLastIndex(users, ['active', false]); // => 2 // The `_.property` iteratee shorthand. _.findLastIndex(users, 'active'); // => 0
1.3、_.indexOf
回傳值(number): 回傳 值value在陣列中的索引位置, 沒有找到為回傳-1,
_.indexOf([1, 2, 1, 2], 2); // => 1 // Search from the `fromIndex`. _.indexOf([1, 2, 1, 2], 2, 2); // => 3
1.4、_.reverse
回傳(Array): 回傳 array.
反轉array,使得第一個元素變為最后一個元素,第二個元素變為倒數第二個元素,依次類推,
var array = [1, 2, 3]; _.reverse(array); // => [3, 2, 1] console.log(array); // => [3, 2, 1]
1.5、_.slice
裁剪陣列array,從 start 位置開始到end結束,但不包括 end 本身的位置,
引數
array(Array): 要裁剪陣列,[start=0](number): 開始位置,[end=array.length](number): 結束位置,
回傳
(Array): 回傳 陣列array 裁剪部分的新陣列,
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/142383.html
標籤:JavaScript
上一篇:js 閉包原理
