jQuery.map()

jQuery.map( array, callback )返回类型:Array

描述:把数组或对象中的所有项翻译为项的新数组。

  • 增补版本:1.0jQuery.map( array, callback )

    • array
      类型:Array
      要翻译的数组。
    • callback
      类型:Function( Object elementOfArray, Integer indexInArray ) => Object
      针对每一项作处理的函数。函数的第一个参数是数组的项,第二个参数是数组中的索引。函数可以返回任何值。一个返回的数组将被压平放入结果数组。在函数内部,this指的是全局(window)对象。
  • 增补版本:1.6jQuery.map( object, callback )

    • object
      类型:Object
      要翻译的对象。
    • callback
      类型:Function( Object propertyOfObject, String key ) => Object
      针对每一项作处理的函数。函数的第一个参数是值,第二个参数是对象属性的键。函数可以返回任何值,以添加到此数组。一个返回的数组将被压平放入结果数组。在函数内部,this指的是全局(window)对象。

如果你想要处理一个jQuery对象——例如,$('div').map( callback );——请使用.map()来代替。

$.map()方法把一个函数应用到数组或对象中的每一项,把结果映射到一个新数组。在jQuery 1.6以前,$.map()只支持遍历数组。从jQuery 1.6以来,它也支持遍历对象。

类数组对象——那些带有.length属性和.length - 1索引上的值的对象——在传递给$.map()之前,必须转换为真实的数组。jQuery库为这样的转换提供了$.makeArray()方法。

1
2
3
4
5
6
7
8
9
10
// The following object masquerades as an array.
var fakeArray = { "length": 2, 0: "Addy", 1: "Subtracty" };
// Therefore, convert it to a real array
var realArray = $.makeArray( fakeArray )
// Now it can be used reliably with $.map()
$.map( realArray, function( val, i ) {
// Do something
});

针对数组或对象中的每个顶层元素调用提供给此方法的翻译函数,翻译函数传入了两个参数:元素的值以及它的在数组中的索引或在对象中的键。

此函数可以返回:

  • 经翻译的值,它将映射到结果数组。
  • nullundefined,以删除该项。
  • 值的数组,它们可被压平到整个数组中。

示例:

使用$.map()以改变数组的值。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery.map demo</title>
<style>
div {
color: blue;
}
p {
color: green;
margin: 0;
}
span {
color: red;
}
</style>
<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
</head>
<body>
<div></div>
<p></p>
<span></span>
<script>
var arr = [ "a", "b", "c", "d", "e" ];
$( "div" ).text( arr.join( ", " ) );
arr = jQuery.map( arr, function( n, i ) {
return ( n.toUpperCase() + i );
});
$( "p" ).text( arr.join( ", " ) );
arr = jQuery.map( arr, function( a ) {
return a + a;
});
$( "span" ).text( arr.join( ", " ) );
</script>
</body>
</html>

演示:

把原来的数组映射到新数组,给每个值加4。

1
2
3
$.map( [ 0, 1, 2 ], function( n ) {
return n + 4;
});

输出结果:

1
[4, 5, 6]

把原来的数组映射到新数组,如果某个值大于0的话,给它加1,如果不大于0,就删除它。

1
2
3
$.map( [ 0, 1, 2 ], function( n ) {
return n > 0 ? n + 1 : null;
});

输出结果:

1
[ 2, 3 ]

把原来的数组映射到新数组;每个元素加上它原来的值,以及该值再加1。

1
2
3
$.map( [ 0, 1, 2 ], function( n ) {
return [ n, n + 1 ];
});

输出结果:

1
[ 0, 1, 1, 2, 2, 3 ]

把原来的对象映射到新数组,并加倍每个值。

1
2
3
4
var dimensions = { width: 10, height: 15, length: 20 };
dimensions = $.map( dimensions, function( value, index ) {
return value * 2;
});

输出结果:

1
[ 20, 30, 40 ]

把一个对象的键映射到一个数组。

1
2
3
4
var dimensions = { width: 10, height: 15, length: 20 };
var keys = $.map( dimensions, function( value, key ) {
return key;
});

输出结果:

1
[ "width", "height", "length" ]

把原来的数组映射到新数组,每个元素乘方。

1
2
3
$.map( [ 0, 1, 2, 3 ], function( a ) {
return a * a;
});

输出结果:

1
[ 0, 1, 4, 9 ]

把原来的数组映射到新数组,通过返回null来删除小于50的数字,并且剩余的减去45。

1
2
3
$.map( [ 0, 1, 52, 97 ], function( a ) {
return (a > 50 ? a - 45 : null);
});

输出结果:

1
[ 7, 52 ]

通过在函数内部返回数组,来增强结果数组。

1
2
3
4
var array = [ 0, 1, 52, 97 ];
array = $.map( array, function( a, index ) {
return [ a - 45, index ];
});

输出结果:

1
[ -45, 0, -44, 1, 7, 2, 52, 3]