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的更多相关文章

  1. ES6,Array.fill()函数的用法

    ES6为Array增加了fill()函数,使用制定的元素填充数组,其实就是用默认内容初始化数组. 该函数有三个参数. arr.fill(value, start, end) value:填充值. st ...

  2. ES6,Array.find()和findIndex()函数的用法

    ES6为Array增加了find(),findIndex函数. find()函数用来查找目标元素,找到就返回该元素,找不到返回undefined. findIndex()函数也是查找目标元素,找到就返 ...

  3. ES6,Array.copyWithin()函数的用法

    ES6为Array增加了copyWithin函数,用于操作当前数组自身,用来把某些个位置的元素复制并覆盖到其他位置上去. Array.prototype.copyWithin(target, star ...

  4. 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数

    有一道经典的字符串处理的问题,统计一个字符串中每个字符出现的次数. 用es6的Array.reduce()函数配合“...”扩展符号可以更方便的处理该问题. s='abananbaacnncn' [. ...

  5. ES6之Array数组

    定义数组 ,]; const arr = new Array(1,2,3,4); const array1 = new Array(); array1[]="test"; 给数组不 ...

  6. ES6,Array.of()函数的用法

    ES6为Array增加了of函数用已一种明确的含义将一个或多个值转换成数组. 因为,用new Array()构造数组的时候,是有二意性的. 构造时,传一个参数,表示生成多大的数组. 构造时,传多个参数 ...

  7. ES6,Array.from()函数的用法

    ES6为Array增加了from函数用来将其他对象转换成数组. 当然,其他对象也是有要求,也不是所有的,可以将两种对象转换成数组. 1.部署了Iterator接口的对象,比如:Set,Map,Arra ...

  8. ES6 — 数组Array

    ES6对数组引入了几个新函数: (1) Array.of() 作用是将一组数值,转换为数组. Array.of(1,2,3,4); //[1,2,3,4] (2) Array.from() 作用是把类 ...

  9. ES6,Array.includes()函数的用法

    在ES5,Array已经提供了indexOf用来查找某个元素的位置,如果不存在就返回-1,但是这个函数在判断数组是否包含某个元素时有两个小不足,第一个是它会返回-1和元素的位置来表示是否包含,在定位方 ...

随机推荐

  1. vue不用webpake等环境及脚手架也可以玩

    初学时,搭环境,es6也来了,vuecil脚手架也弄了,调错,照着教程一遍一遍kei着... 然尔,实际开发中,所写的东西最后是要打包封装成软件的,为了方便其他人修改查看,不能打包成js文件... 难 ...

  2. 深入理解String.intern()方法

    首先进入intern()的源码中, 首先说一点:1.7后的JVM为String在方法区中开辟了一个字符串常量池,如果一个String()不是new()出来的,都将在常量池中拿字符. 注释翻译过来就是, ...

  3. 51nod 1295 XOR key-区间异或最大值-可持久化01Trie树(模板)

    1295 XOR key 2 秒 262,144 KB 160 分 6 级题   给出一个长度为N的正整数数组A,再给出Q个查询,每个查询包括3个数,L, R, X (L <= R).求A[L] ...

  4. 30、Flask实战第30天:cms模版抽离和个人信息页面完成

    cms模版抽离 新建一个cms_base.html文件作为基础模板,把cms_index.html的内容拷贝到cms_base.html中. 编辑 cms_base.html,把在不同页面会变动的部分 ...

  5. oracle return code 2112

    SQL-02112 SELECT..INTO returns too many rows Cause: A SELECT...INTO statement returned more rows tha ...

  6. eclipse汉化 adt汉化

    韩梦飞沙  韩亚飞  313134555@qq.com  yue31313  han_meng_fei_sha

  7. 【HDU 5730】Shell Necklace

    http://acm.hdu.edu.cn/showproblem.php?pid=5730 分治FFT模板. DP:\(f(i)=\sum\limits_{j=0}^{i-1}f(j)\times ...

  8. 「2018山东一轮集训」 Tree

    为什么出题人这么毒瘤啊??!!一个分块还要带log的题非要出成n<=2*1e5....... 为了卡过最后两个点我做了无数常数优化,包括但不限于:把所有线段树改成 存差分的树状数组:把树剖求LC ...

  9. 【高斯消元解xor方程组】BZOJ2466-[中山市选2009]树

    [题目大意] 给出一棵树,初始状态均为0,每反转一个节点的状态,相邻的节点(父亲或儿子)也会反转,问要使状态均为1,至少操作几次? [思路] 一场大暴雨即将来临,白昼恍如黑夜!happy! 和POJ1 ...

  10. Codeforces Round #345 (Div. 1) E. Clockwork Bomb 并查集

    E. Clockwork Bomb 题目连接: http://www.codeforces.com/contest/650/problem/E Description My name is James ...