[ES6] 10. Array Comprehensions
ES6 decided that Array Comprehensions will not included in this version, ES7 will include this. Therefore, only FireFox support this, you can test it under FireFox Firebug.
Since CoffeeScrip also has array comprehension, let's go over how it looks in CoffeeScript first:
for person in people
console.log(person.name) if person.age > 18
console.log "#{person.name}" for person in people when person.age > 18
// In coffeeScrip,
//<expression> for <elm> in <list> when <elm> <condition>
Read More: http://www.cnblogs.com/Answer1215/p/4002666.html
Array Comprehensions:
var people = [
{
firstName: "Allen",
secondName: "Hook",
email: "allen@abc.com"
},
{
firstName: "Anton",
secondName: "Tee",
email: "tee@abc.com"
},
{
firstName: "Yui",
secondName: "Gegg",
email: "gegg@abc.com"
}
];
For example, I have a list of people, and I want to get all the emails into a spreate array:
var emails = [for(person of people) person.email];
console.log(emails);
If:
you can add if segement as well after for..of:
var emails = [for(person of people) if(person.firstName === "Yui") person.email];
console.log(emails); // ["gegg@abc.com"]
Mutli for..of:
var nums = [1, 2, 3, 4, 5];
var letters = ["a", "b", "c", "d", "e"] var battleship = [for(num of nums) for(letter of letters) num + letter] console.log(battleship);
// ["1a", "1b", "1c", "1d", "1e", "2a", "2b", "2c", "2d", "2e", "3a", "3b", "3c", "3d", "3e", "4a", "4b", "4c", "4d", "4e", "5a", "5b", "5c", "5d", "5e"]
var nums = [1, 2, 3, 4, 5];
var letters = ["a", "b", "c", "d", "e"] var battleship = [for(num of nums) [for(letter of letters) num + letter]] console.log(battleship); // [["1a", "1b", "1c", 2 more...], ["2a", "2b", "2c", 2 more...], ["3a", "3b", "3c", 2 more...], ["4a", "4b", "4c", 2 more...], ["5a", "5b", "5c", 2 more...]]
String can also be considered as an 'array':
let letter = [for (letter of 'abcde') if (/[aeiou]/.test(letter)) letter].join('');
console.log(letter); // ae
[ES6] 10. Array Comprehensions的更多相关文章
- ES6,Array.fill()函数的用法
ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组. 该函数有三个参数. arr.fill(value, start, end) value:填充值. st ...
- ES6,Array.find()和findIndex()函数的用法
ES6为Array增加了find(),findIndex函数. find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined. findIndex()函数也是查找目标元素,找到就返 ...
- ES6,Array.copyWithin()函数的用法
ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去. Array.prototype.copyWithin(target, star ...
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...
- ES6之Array数组
定义数组 ,]; const arr = new Array(1,2,3,4); const array1 = new Array(); array1[]="test"; 给数组不 ...
- ES6,Array.of()函数的用法
ES6为Array增加了of函数用已一种明确的含义将一个或多个值转换成数组. 因为,用new Array()构造数组的时候,是有二意性的. 构造时,传一个参数,表示生成多大的数组. 构造时,传多个参数 ...
- ES6,Array.from()函数的用法
ES6为Array增加了from函数用来将其他对象转换成数组. 当然,其他对象也是有要求,也不是所有的,可以将两种对象转换成数组. 1.部署了Iterator接口的对象,比如:Set,Map,Arra ...
- ES6 — 数组Array
ES6对数组引入了几个新函数: (1) Array.of() 作用是将一组数值,转换为数组. Array.of(1,2,3,4); //[1,2,3,4] (2) Array.from() 作用是把类 ...
- ES6,Array.includes()函数的用法
在ES5,Array已经提供了indexOf用来查找某个元素的位置,如果不存在就返回-1,但是这个函数在判断数组是否包含某个元素时有两个小不足,第一个是它会返回-1和元素的位置来表示是否包含,在定位方 ...
随机推荐
- websocket小荔枝
关于websocket原理和好处请参考百度,这里只是代码. var ws = new WebSocket('ws://echo.websocket.org/'); ws.onopen = functi ...
- swiper使用心得
引入: <link rel="stylesheet" href="https://cdn.bootcss.com/Swiper/3.4.2/css/swiper.m ...
- CentOS7安装和配置vsftpd
(1)vsftpd基本介绍 作用:实现文件共享 1)vsftpd两种模式 主动模式 所谓主动模式,指的是FTP服务器"主动"去连接客户端的数据端口来传输数据,其过程具体来说就是:客 ...
- AtCoder Regular Contest 103 Problem D Robot Arms (构造)
题目链接 Problem D 给定$n$个坐标,然后让你构造一个长度为$m$的序列, 然后给每个坐标规定一个长度为$m$的序列,ULRD中的一个,意思是走的方向, 每次从原点出发按照这个序列方向,每 ...
- Java HashSet的元素内容变化导致的问题
概述 HashSet元素引用的对象的内容发生变化,会导致"元素不属于集合"的问题.事实上这个元素还在集合里,但是调用contains方法进行判断,得到的结果却是false. 正文 ...
- BZOJ 2073 [POI2004]PRZ(状压DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2073 [题目大意] 任何时候队伍在桥上的人都不能超过一定的限制. 所以这只队伍过桥时只 ...
- BZOJ 4027:[HEOI2015]兔子与樱花(贪心+树形DP)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4027 [题目大意] 樱花树由n个树枝分叉点组成,编号从0到n-1,这n个分叉点由n-1 ...
- 【概率dp】【数学期望】Gym - 101190F - Foreign Postcards
http://blog.csdn.net/DorMOUSENone/article/details/73699630
- hibernate双向ManyToMany映射
工作需要一个双向多对多映射,照着李刚的书做了映射,碰到了一些问题,现就问题及解决方案进行总结归纳. 1.首先奉上最初代码 Person5.java @Entity @Table(name = &quo ...
- 时间同步Servname not supported for ai_socktype
rdate -s 129.7.1.66rdate: 129.7.1.66: Servname not supported for ai_socktype ntpdate 0.centos.pool.n ...