1
0
mirror of https://github.com/ruanyf/es6tutorial.git synced 2025-05-25 11:12:21 +00:00
es6tutorial/docs/array.md
2014-05-08 13:06:00 +08:00

95 lines
2.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# 数组的扩展
## Array.prototype.find()Array.prototype.findIndex()
Array.prototype.find()用于找出第一个符合条件的数组元素。它的参数是一个回调函数所有数组元素依次遍历该回调函数直到找出第一个返回值为true的元素。
```javascript
[1, 5, 10, 15].find(function(value, index, arr) {
return a > 9;
}) // 10
```
从上面代码可以看到,回调函数接受三个参数,依次为当前的值、当前的位置和原数组。
Array.prototype.findIndex()的用法与find()非常类似,返回第一个符合条件的数组元素的位置。
```javascript
[1, 5, 10, 15].findIndex(function(value, index, arr) {
return a > 9;
}) // 2
```
## 数组推导
ES6提供简洁写法允许直接通过现有数组生成新数组这被称为数组推导array comprehension
```javascript
var a1 = [1, 2, 3, 4];
var a2 = [i * 2 for (i of a1)];
a2 // [2, 4, 6, 8]
```
上面代码表示通过for...of结构数组a2直接在a1的基础上生成。
数组推导可以替代map和filter方法。
```javascript
[for (i of [1, 2, 3]) i * i];
// 等价于
[1, 2, 3].map(function (i) { return i * i });
[i for (i of [1,4,2,3,-8]) if (i < 3)];
// 等价于
[1,4,2,3,-8].filter(function(i) { return i < 3 });
```
上面代码说明模拟map功能只要单纯的for...of循环就行了模拟filter功能除了for...of循环还必须加上if语句。
新引入的for...of结构可以直接跟在表达式的前面或后面甚至可以在一个数组推导中使用多个for...of结构。
```javascript
var a1 = ["x1", "y1"];
var a2 = ["x2", "y2"];
var a3 = ["x3", "y3"];
[(console.log(s + w + r)) for (s of a1) for (w of a2) for (r of a3)];
// x1x2x3
// x1x2y3
// x1y2x3
// x1y2y3
// y1x2x3
// y1x2y3
// y1y2x3
// y1y2y3
```
上面代码在一个数组推导之中使用了三个for...of结构。
需要注意的是数组推导的方括号构成了一个单独的作用域在这个方括号中声明的变量类似于使用let语句声明的变量。
由于字符串可以视为数组,因此字符串也可以直接用于数组推导。
```javascript
[c for (c of 'abcde') if (/[aeiou]/.test(c))].join('') // 'ae'
[c+'0' for (c of 'abcde')].join('') // 'a0b0c0d0e0'
```
上面代码使用了数组推导,对字符串进行处理。
数组推导需要注意的地方是,新数组会立即在内存中生成。这时,如果原数组是一个很大的数组,将会非常耗费内存。