当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


JQuery jQuery.map()用法及代码示例


用法
jQuery.map( array, callback ) => Array

说明:将数组或对象中的所有项目转换为新的项目数组。

  • 添加的版本:1.0jQuery.map( array, callback )

    • array
      类型:Array
      要翻译的数组。
    • callback
      类型:Function (Object elementOfArray, Integer indexInArray) => Object
      处理每个项目的函数。函数的第一个参数是数组项,第二个参数是数组中的索引 函数可以返回任何值。返回的数组将被展平为结果数组。在函数中,this 指的是全局(窗口)对象。
  • 添加的版本:1.6jQuery.map( object, callback )

    • object
      类型:Object
      要翻译的对象。
    • callback
      类型:Function (Object propertyOfObject, String key) => Object
      处理每个项目的函数。函数的第一个参数是值;第二个参数是对象属性的键。该函数可以返回任何值以添加到数组中。返回的数组将被展平为结果数组。在函数中,this 指的是全局(窗口)对象。

如果您希望处理一个 jQuery 对象——例如,$('div').map( callback );——请改用.map()

$.map()方法将函数应用于数组或对象中的每个项目,并将结果映射到新数组中。在 jQuery 1.6 之前,$.map()支持遍历arrays only.从 jQuery 1.6 开始它还遍历对象。

Array-like 对象 — 具有 .length 属性 and.length - 1 索引上的值的对象 — 在传递给 $.map() 之前必须转换为实际数组。 jQuery 库为此类转换提供了$.makeArray()

// 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() 更改数组的值。

<!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-3.5.0.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。

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

结果:

[4, 5, 6]

将原始数组映射到一个新数组,如果每个值大于零,则将其加 1,否则将其删除。

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

结果:

[ 2, 3 ]

将原始数组映射到新数组;每个元素都加上它的原始值和加一的值。

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

结果:

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

将原始对象映射到新数组并将每个值加倍。

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

结果:

[ 20, 30, 40 ]

将对象的键映射到数组。

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

结果:

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

将原始数组映射到新数组;每个元素都是平方的。

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

结果:

[ 0, 1, 4, 9 ]

将原始数组映射到新数组,通过返回 null 并从其余部分中减去 45 来删除小于 50 的数字。

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

结果:

[ 7, 52 ]

通过在函数内返回一个数组来扩充结果数组。

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

结果:

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

相关用法


注:本文由纯净天空筛选整理自jquery.com大神的英文原创作品 jQuery.map()。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。