lodash常用函数 - Array、Collection

lodash版本 v3.10.1

1.Array、Collection

pull

移除数组中满足条件的元素

var array = [1, 2, 3, 1, 2, 3];
_.pull(array, 2, 3);
// => [1, 1]

slice

从start位置到 end(但不包含end位置),截取 array数组

_.slice([1, 2, 3, 1],2,3)
// => [3]

difference

排除第一个数组中不包含第二个数组的值

_.difference([1, 2, 3], [4, 2]);
// => [1, 3]

findIndex

返回符合条件的元素的索引,没有找到返回-1

var users = [
{ 'user': 'barney', 'active': false },
{ 'user': 'fred', 'active': false },
{ 'user': 'pebbles', 'active': true }
];
_.findIndex(users, 'active', false);
// => 0

drop

将数组中的前n(默认是1)个元素去掉,返回剩余部分

_.drop([1, 2, 3], 2);
// => [3]

xor

取出各数组中不等的元素

_.xor([1, 2], [4, 2]);
// => [1, 4]

intersection

取出各数组中全等的元素

_.intersection([1, 2], [4, 2], [2, 1]);
// => [2]

remove

删掉满足条件的元素

var array = [{name:'年龄',value:'34'},{name:'年龄',value:'20'},{name:'年龄',value:''}];
_.remove(array, function(item) {
return item.value == '';
})
//[{name:'年龄',value:'34'},{name:'年龄',value:'20'}]

union

合并各数组的值,返回新数组

_.union([1, 2], [4, 2], [2, 1]);
// => [1, 2, 4]

uniq

去除数组中的重复元素

_.uniq([2, 1, 2]);
// => [2, 1]

pluck

取出json数组中某个属性的值,组成一个新数组返回

var users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 }
]; _.pluck(users, 'user');
// => ['barney', 'fred']

zipObject

由两个数组的元素配对组成一个新的对象

_.zipObject(['fred', 'barney'], [30, 40]);
// => { 'fred': 30, 'barney': 40 }

filter

以新数组形式返回数组中满足条件的元素

_.filter([4, 5, 6], function(n) {
return n % 2 == 0;
});
// => [4, 6] var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.pluck(_.filter(users, 'active', false), 'user');
// => ['fred']

find

返回数组中第一个满足条件的元素

var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false },
{ 'user': 'pebbles', 'age': 1, 'active': true }
]; _.result(_.find(users, function(chr) {
return chr.age < 40;
}), 'user');
// => 'barney' // using the `_.matches` callback shorthand
_.result(_.find(users, { 'age': 1, 'active': true }), 'user');
// => 'pebbles' // using the `_.matchesProperty` callback shorthand
_.result(_.find(users, 'active', false), 'user');
// => 'fred' // using the `_.property` callback shorthand
_.result(_.find(users, 'active'), 'user');
// => 'barney'

includes

判断元素是否被包含在目标集合中

_.includes([1, 2, 3], 1);
// => true _.includes([1, 2, 3], 1, 2);
// => false _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
// => true _.includes('pebbles', 'eb');
// => true

indexBy

从集合中获取某一个属性值生成新对象,属性值为新对象的键

var keyData = [
{ 'dir': 'left', 'code': 97 },
{ 'dir': 'right', 'code': 100 }
]; _.indexBy(keyData, 'dir');
// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }

sample

从集合中随机获取n个元素,n默认为1

_.sample([1, 2, 3, 4]);
// => 2 _.sample([1, 2, 3, 4], 2);
// => [3, 1]

size

返回目标集合的长度

_.size([1, 2, 3]);
// => 3 _.size({ 'a': 1, 'b': 2 });
// => 2 _.size('pebbles');
// => 7

sortBy

根据特定规则对集合进行排序

var users = [
{ 'user': 'fred' },
{ 'user': 'pebbles' },
{ 'user': 'barney' }
]; _.pluck(_.sortBy(users, 'user'), 'user');
// => ['barney', 'fred', 'pebbles']

lodash常用函数 - Array、Collection的更多相关文章

  1. php常用数组array函数实例总结【赋值,拆分,合并,计算,添加,删除,查询,判断,排序】

    本文实例总结了php常用数组array函数.分享给大家供大家参考,具体如下: array_combine 功能:用一个数组的值作为新数组的键名,另一个数组的值作为新数组的值 案例: <?php ...

  2. PHP常用数组(Array)函数整理

    整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数.数组的分段和填充.数组与栈.数组与列队.回调函数.排序.计算.其他的数组函数等. 一.数组操作的基本函数 数组的键名和值 array_va ...

  3. 【PHP】PHP常用数组(Array)函数整理

    整理了一份PHP开发中数组操作大全,包含有数组操作的基本函数.数组的分段和填充.数组与栈.数组与列队.回调函数.排序.计算.其他的数组函数等. 一.数组操作的基本函数 数组的键名和值 array_va ...

  4. Hive常用函数的使用

    Hive常用函数的使用 文章作者:foochane  原文链接:https://foochane.cn/article/2019062501.html 1 基本介绍 1.1 HIVE简单介绍 Hive ...

  5. hive常用函数 wordCount--Hive窗口函数1.1.1 聚合开窗函数聚合开窗函数实战

    第三天笔记 第三天笔记 SQL练习Hive 常用函数关系运算数值计算条件函数日期函数重点!!!字符串函数Hive 中的wordCount1.1 Hive窗口函数1.1.1 聚合开窗函数聚合开窗函数实战 ...

  6. Thinkcmf:页面常用函数

    Thinkcmf:页面常用函数 全站seo: 文章列表: {$site_seo_title}        <!--SEO标题--> {$site_seo_keywords}   < ...

  7. phpcms V9 常用函数 及 代码整理

    常用函数 及 常用代码 总结如下 <?php //转换字符串或者数组的编码 str_charset($in_charset, $out_charset, $str_or_arr) //获取菜单 ...

  8. php : 常用函数

    常用函数: <?php /** * 获取客户端IP * @return [string] [description] */ function getClientIp() { $ip = NULL ...

  9. php 数组的常用函数

    在php教程中数组是种强大的数据类型,他可以做的事情很多,可以存储不同的数据类型在一个数组中,下面我们列出了数组常用的操作,排序,键名对数组排序等做法. /* 数组的常用函数  *  * 数组的排序函 ...

随机推荐

  1. iis+php(FastCGI)

    1. 安装 IIS 时选择添加 CGI 功能 2. 安装 PHP, 2.1 下载 nts 版本 (非线程安全版本) zip 压缩包,下载对应的 vc++ 运行时(php官网下载页面左侧有下载链接) 2 ...

  2. C# winform窗体传值 利用委托 子窗体传值给父窗体

    首先在Form2中定义委托和事件: //声明委托 和 事件 public delegate void TransfDelegate(String value); public partial clas ...

  3. npm 包管理工具

    能注册后看简单的功能 订单加信息 下单之前的判断要配合海潮的迁移数据 运行自定义的脚本 在 package.json 的 scripts 里添加自定义的结点 ( 比如 CSOR-serve ) &qu ...

  4. Centos6.5上安装sonarqube6.7.6

    Centos6.5已经装备好,可以联网 sonarqube6.7.6下载地址:https://www.sonarqube.org/downloads/ 步骤: 安装MySQL5.7 MySQL详细的安 ...

  5. Java框架-mybatis02基本的crud操作

    1.搭建mybatis框架 1)导入相关jar包 2)编写核心配置文件(配置数据库连接的相关信息以及配置mapper映射文件) 3)编写dao操作 4)编写mapper映射文件 5)编写实体类 2.执 ...

  6. openstf安装手记

    一款基于nodejs+jade模板开发的手机群控框架,你可以在此基础上拿来测试设备,或者群操作等等. 主要核心技术在于如何在不root的情况下实时流畅录屏,以及远程操作手机. 官方git https: ...

  7. Java学习之路(四):面向对象

    Java中的面向对象 概念:面向对象的原本的意思是“”万物皆对象“” 面向对象思想的特点: 是一种更符合我们思想习惯的思想,将复杂的事情简单化 使我们角色发生了转换,将我们从执行者变成了指挥者 面向对 ...

  8. gson日期转换问题

    转:http://blog.csdn.net/liao_leo/article/details/44593095 今天遇到个很奇怪的问题,gson解析日期字符串,本地执行可以,服务器上执行就报错. 这 ...

  9. 安装和部署Jenkins

    安装和部署Jenkins 环境 操作系统:ubuntu 14.04.4 LTS 下载Jenkins wget https://mirrors.tuna.tsinghua.edu.cn/jenkins/ ...

  10. FocusBI: 使用Python爬虫为BI准备数据源(原创)

    关注微信公众号:FocusBI 查看更多文章:加QQ群:808774277 获取学习资料和一起探讨问题. <商业智能教程>pdf下载地址 链接:https://pan.baidu.com/ ...