jquery.map使用方法
jQuery.map(array,callback)
将一个数组中的元素转换到另一个数组中。
作为参数的转换函数会为每个数组元素调用,而且会给这个转换函数传递一个表示被转换的元素作为参数。转换函数可以返回转换后的值、null(删除数组中的项目)或一个包含值的数组,并扩展至原始数组中。
--------------------------------------------------------------------------------
Translate all items in an array to another array of items. The translation function that is provided to this method is called for each item in the array and is passed one argument: The item to be translated. The function can then return the translated value, 'null' (to remove the item), or an array of values - which will be flattened into the full array.
返回值
Array
参数
array (Array) : 待转换数组。
callback (Function) : 为每个数组元素调用,而且会给这个转换函数传递一个表示被转换的元素作为参数。函数可返回任何值。另外,此函数可设置为一个字符串,当设置为字符串时,将视为“lambda-form”(缩写形式?),其中 a 代表数组元素。如“a * a”代表“function(a){ return a * a; }”。
示例
将原数组中每个元素加 4 转换为一个新数组。
jQuery 代码:
$.map( [0,1,2], function(n){
return n + 4;
});结果:
[4, 5, 6]
--------------------------------------------------------------------------------
原数组中大于 0 的元素加 1 ,否则删除。
jQuery 代码:
$.map( [0,1,2], function(n){
return n > 0 ? n + 1 : null;
});结果:
[2, 3]
--------------------------------------------------------------------------------
原数组中每个元素扩展为一个包含其本身和其值加 1 的数组,并转换为一个新数组。
jQuery 代码:
$.map( [0,1,2], function(n){
return [ n, n + 1 ];
});结果:
[0, 1, 1, 2, 2, 3]
过滤数组中小于 0 的元素。
HTML 代码:
First
Second
Third
Fourth
jQuery 代码:
var arr = jQuery.makeArray(document.getElementsByTagName_r("p"));
arr.reverse(); // 使用数组翻转函数结果:
Fourth Third Second First
过滤数组中小于 0 的元素。
rebuild是一款高度可配置化的企业管理系统!可免费商用!低代码/零代码快速搭建企业中台、OA办公自动化、CRM客户关系管理、WMS库存管理、TMS运输管理、SCM供应链管理,甚至是 ERP 企业资源计划!REBUILD 侧重于业务需求实现,而非基础技术框架或项目启动模板,通过 REBUILD 可以真正实现零代码快速搭建,无需编程、无需编译代码,甚至无需了解技术。 使用开始使用 REBUIL
HTML 代码:
First
Second
Third
Fourth
jQuery 代码:
var arr = jQuery.makeArray(document.getElementsByTagName_r("p"));
arr.reverse(); // 使用数组翻转函数结果:
Fourth Third Second First
过滤数组中小于 0 的元素。
HTML 代码:
First
Second
Third
Fourth
jQuery 代码:
var arr = jQuery.makeArray(document.getElementsByTagName_r("p"));
arr.reverse(); // 使用数组翻转函数结果:
Fourth Third Second First
附:过滤数组中小于 0 的元素。
HTML 代码:
First
Second
Third
Fourth
jQuery 代码:
var arr = jQuery.makeArray(document.getElementsByTagName_r("p"));
arr.reverse(); // 使用数组翻转函数结果:
Fourth Third Second First









