lodash 安装及使用

lodash:中文版网站:https://www.lodashjs.com/
借用 中文版说明:
Lodash 是一个一致性、模块化、高性能的 JavaScript 实用工具库。

lodash( 数据优先,函数之后)

使用及安装:
1. 在项目 终端 先初始化  npm init -y
2.安装 lodash  :  npm  install lodash
3.下面看代码

// 1.npm init -y
// 2.npm install lodash
// 3.引入 lodash
const lodash = require("lodash")

// 4.定义数组
let  arr  =  ["a","b","c","d","e"]

// 5. 方法及使用  first / last / toUpper / reverse / each / includes / find / findIndex

console.log( lodash.first(arr) );   //  first 取数组第一个值
console.log( lodash.last(arr) );    //  last 取数组最后一个值
console.log( lodash.toUpper(arr) ); //  将数组值改为大写
console.log( lodash.toUpper(lodash.first(arr)) );  //将数组第一个值大写
console.log( lodash.reverse(arr) ); //  将数组内容进行反转
console.log( lodash.reverse(arr) );
console.log( lodash.each(arr) );    //  将数组进行循环


// se6 之后的三个方法

console.log( lodash.includes(arr,"a") );    //  用于查找 这个值是否存在,存在为true ,否则为false

var user = [
    { 'user': 'barney',  'age': 36, 'active': true },
    { 'user': 'fred',    'age': 40, 'active': false },
    { 'user': 'pebbles', 'age': 1,  'active': true }
  ];
  console.log( lodash.find(arr) ); // 第一个返回真值的第一个元素 a
console.log( lodash.find(arr, function(item) { return  item==="e"}) ); // 先循环一下,找到相同的值,有打印出来,没有就undefined
console.log( lodash.find(arr, function(item) { return  item==="a"},0) ); //同上,不过多加一个参数,开始搜索的索引位置
console.log( lodash.findIndex(arr) ); // findIndex 改方法类似find,返回是元素的索引值


 



 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值