【JavaScript】Lodash在React Native中的使用


  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 本身的位置。

  參數

  1. array (Array): 要裁剪數組。
  2. [start=0] (number): 開始位置。
  3. [end=array.length] (number): 結束位置。

  返回

  (Array): 返回 數組array 裁剪部分的新數組。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM