---
title: "Apply和数组:三个技巧 (转)"
author: "zhoon"
date: "2012-09-22T22:19:43Z"
tags: []
categories: ["Web前端"]
slug: "apply"
draft: false
lastmod: "2012-09-22T22:19:43Z"
id: 1683634
url: "https://zhoon.iteye.com/blog/1683634"
---

本文要讲的是:使用`apply方法处理数组的三个技巧` .

## apply方法

`apply是所有函数都有的方法` .它的签名如下:

```
func.apply(thisValue, [arg1, arg2, ...])
```

`如果不考虑thisValue的影响` ,上面的调用等同于:

```
func(arg1, arg2, ...)
```

也就是说,`apply允许我们将一个数组"解开"成为一个个的参数再传递给调用函数` .让我们分别看看`apply使用中的三个技巧.`

## 技巧1: 将一个数组传递给一个不接受数组作为参数的函数

JavaScript中没有返回一个数组中最大值的函数.但是,有一个函数`Math.max可以返回任意多个数值类型的参数中的最大值` .再配合`apply` ,我们可以实现我们的目的:

```
> Math.max.apply(null, [10, -1, 5])
10
```

译者注:注意Math.max方法的参数中只要有一个值被转为NaN,则该方法直接返回NaN

```
>Math.max(1,null
)  //
相当于Math.max(1,0)

1
>Math.max(1,undefinded)  //
相当于Math.max(1,NaN)

NaN


>Math.max(0,-0)   //
正零比负零大,和==不同

0
>Math.max(-0,-1)  //
负零比-1大


-0
```

## 技巧2: 填补稀疏数组

#### 数组中的缝隙

这里提醒一下读者:在JavaScript中,一个数组就是一个数字到值的映射.所以如果某个索引处缺失了一个元素(一条缝隙)和某个元素的值为`undefined` ,是两种不同的情况.前者在被`Array.prototype中的相关方法` (`forEach` ,  `map` , 等.)遍历时,会跳过那些缺失的元素,而后者不会:

```
> ["a",,"b"].forEach(function
 (x) { console.log(x) })
a
b


> ["a",undefined,"b"].forEach(function
 (x) { console.log(x) })
a
undefined
b
```

译者注:这里作者说"数组就是一个数字到值的映射",严格意义上是不对的,正确的说法是"数组就是一个字符串到值的映射".下面是证据:

```
>for (i in ["a", "b"]) {

    console.log(typeof i)  //数组的索引实际上是个字符串

 }

"string"

"string"


>["a", "b"].forEach(function (x, i) {

     console.log(typeof i) //这里的i实际上不是索引,只是个数字类型的累加器


 })

"number
"

"number
"
```

你可以使用`in运算符来检测数组中是否有缝隙` .

```
> 1 in
 ["a",,"b"]

false

> 1 in
 ["a", undefined, "b"]

true
```

译者注:这里之所以用1可以,是因为in运算符会把1转换成"1".

你过你尝试读取这个缝隙的值,会返回`undefined,和实际的` `undefined元素是一样` .

```
> ["a",,"b"][1]
undefined

> ["a", undefined, "b"][1]
undefined
```

译者注:\[1]也会被转换成\["1"]

#### 填补缝隙

`apply配合` `Array` (这里不需要加new)使用,可以将数组中的缝隙填补为`undefined` 元素:

```
> Array.apply(null, ["a",,"b"])
[ 'a', undefined, 'b' ]
```

这都是因为`apply不会忽略数组中的缝隙` ,会把缝隙作为`undefined参数 传递给函数` :

```
> function
 returnArgs() { return
 [].slice.call(arguments) }

> returnArgs.apply(null
, ["a",,"b"])
[ 
'a', undefined, 'b' ]
```

但需要注意的是,如果`Array方法接收到的参数是一个单独的数字,则会把这个参数当成数组长度,返回一个新数组` :

```
> Array.apply(null
, [ 3 ])
[ , ,  ]
```

因此,最靠谱的方法是写一个这样的函数来做这种工作:

```
function
 fillHoles(arr) {
    
var
 result = [];
    
for
(var
 i=0; i < arr.length; i++) {
        result[i] 
= arr[i];
    }
    
return
 result;
}
```

执行:

```
> fillHoles(["a",,"b"])
[ 
'a', undefined, 'b' ]
```

Underscore中的[`_.compact`](http://cc.c-yark.com/browse.php?u=2d1c9ded505b6Oi8vdW5kZXJzY29yZWpzLm9yZy8%3D&b=5#compact) 函数会移除数组中的所有假值,包括缝隙:

```
> _.compact(["a",,"b"])
[ 
'a', 'b' ]

> _.compact(["a", undefined, "b"])
[ 
'a', 'b' ]

> _.compact(["a", false
, "b"])
[ 
'a', 'b' ]
```

## 技巧3:  扁平 化 数组

任务:将一个包含多个数组元素的数组转换为一个一阶数组.我们利用`apply解包数组的能力配合` `concat来做这件事` :

```
> Array.prototype.concat.apply([], [["a"], ["b"]])
[ 
'a', 'b' ]
```

混合非数组类型的元素也可以:

```
> Array.prototype.concat.apply([], [["a"], "b"])
[ 
'a', 'b' ]
```

`apply` 方法的`thisValue必须指定为` `[]` ,因为`concat是一个数组的方法` ,不是一个独立的函数.这种写法的限制是最多只能扁平化二阶数组:

```
> Array.prototype.concat.apply([], [[["a"]], ["b"]])
[ [ 
'a' ], 'b' ]
```

所以你应该 考虑一个替代方案 .比如Underscore中的[\_.flatten](http://underscorejs.org/#flatten) 函数就可以处理任意层数的 嵌套数组 ：

```
> _.flatten([[["a"]], ["b"]])
[ 
'a', 'b' ]
```

 

原文地址： [http://www.cnblogs.com/ziyunfei/archive/2012/09/18/2690412.html](http://www.cnblogs.com/ziyunfei/archive/2012/09/18/2690412.html)