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. 使用css中的flex布局弹性手风琴效果

    不多说,直接上代码. <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  2. lr_Vugen界面图

  3. mp4文件DASH切片程序

    mp4文件DASH切片程序 一.简介 按照DASH标准文档要求与现有的DASH切片(生成DASH切片参见mb4box命令简介)来生成Initialization Segment与Media Segme ...

  4. 【Bzoj4555】【Luogu P4091】求和(NTT)

    题面 Bzoj Luogu 题解 先来颓柿子 $$ \sum_{i=0}^n\sum_{j=0}^iS(i,j)2^jj! \\ =\sum_{j=0}^n2^jj!\sum_{i=0}^nS(i,j ...

  5. 洛谷——P1529 回家 Bessie Come Home

    P1529 回家 Bessie Come Home 题目描述 现在是晚餐时间,而母牛们在外面分散的牧场中. 农民约翰按响了电铃,所以她们开始向谷仓走去. 你的工作是要指出哪只母牛会最先到达谷仓(在给出 ...

  6. curator管理es索引

    安装curator------------------rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch vi /etc/yu ...

  7. 杭电oj 1002

    #include <iostream> #include <algorithm> using namespace std; int nCases; ], n[]; ], b[] ...

  8. Spring的事务传播性

    事务是逻辑处理原子性的保证手段,通过使用事务控制,可以极大的避免出现逻辑处理失败导致的脏数据等问题.事务最重要的两个特性,是事务的传播级别和数据隔离级别.传播级别定义的是事务的控制范围,事务隔离级别定 ...

  9. JZYZOJ1457 [NOIP2016]换教室 期望dp 动态规划 floyd算法 最短路

    http://172.20.6.3/Problem_Show.asp?id=1457 我不知道为什么我倒着推期望只有80分,所以我妥协了,我对着题解写了个正的,我有罪. #include<cst ...

  10. 【树形dp】Find Metal Mineral

    [HDU4003]Find Metal Mineral Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (J ...