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. JSTL 1.1与JSTL 1.2之间的区别?如何下载JSTL 1.2?

    JSTL 1.1与JSTL 1.2之间的区别?如何下载JSTL 1.2? JSTL 1.2中不要求standard.jar架包 您可以在Maven中央仓库中找到它们: http://repo2.mav ...

  2. 再聊语言,模式,OOD

    今天与人再次聊到这个话题,有人在为"到底该用什么模式"而烦恼,我相信,每个都经历过这个阶段一定都会感觉很熟悉这个烦恼我认为, 模式不是目的,只是工具,达到设计目标的工具,我们不会因 ...

  3. 记一次Python爬虫开发经历

    为啥要做Python爬虫,是因为我去找电影的某个网站有点坑,它支持tag标签查询自己喜欢的电影,但是不支持双标签或者三标签查询.由于一个电影对应多种类型(tag),这就意味着,我需要进入这个电影介绍界 ...

  4. 第7天-javascript内置对象

    数组相关方法 concat 用来连接多个数组 <script> var a = [1,2,3]; var b = [3,4,5]; var c = a.concat(b); console ...

  5. 微软移除Visual Studio 2015中的UML

    微软已经在Visual Studio 2015中移除了UML(Unified Modeling Language,统一建模语言),原因是该语言使用率过低.因此微软要优化产品结构,把好钢用在刀刃上. V ...

  6. Abp数据库迁移注意事项

    前记:昨天下载了一个Abp模板,然后尝试利用EF CodeFirst进行数据库生成操作,然后就是一直报错 在与 SQL Server 建立连接时出现与网络相关的或特定于实例的错误.未找到或无法访问服务 ...

  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. DZY Loves Chinese / DZY Loves Chinese II

    题面在这里! 这两个其实是一个题啦..双倍经验加成23333 可以很简单的发现如果把一条树边和所有覆盖它的非树边都删去的话,那么图会不连通: 如果再手玩一下可以发现,如果把两个被非树边覆盖的集合相同的 ...

  9. Mobiscroll手机触屏日期选择器

       最近在制作jquery mobile因要用到日历控件,突然发现Mobiscroll非常不错.于是摘下来记录. A Mobiscroll是一个用于触摸设备(Android phones.iPhon ...

  10. Problem D: 零起点学算法94——输出矩阵

    #include<stdio.h> int main() { ][]; while(scanf("%d %d",&n,&m)!=EOF) { ; ;i& ...