当前位置: 移动技术网 > IT编程>开发语言>JavaScript > 【JavaScript】Lodash在React Native中的使用

【JavaScript】Lodash在React Native中的使用

2020年03月13日  | 移动技术网IT编程  | 我要评论

  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 裁剪部分的新数组。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网