javascript - 将 jQuery map 函数转换为箭头函数

标签 javascript jquery

我正在尝试将以下工作函数转换为箭头函数,但失败了。我不知道我做错了什么。

这是我的起始情况:

let abc = Math.max.apply( null, $( "#wrapper .item" ).map( function () {
    return parseInt( $( this ).prop( "id" ).match( /\d+/g ), 10 );
} ) );

这就是我尝试过的。也许我误解了什么?:

let abc = Math.max.apply( null, $( "#wrapper .item" ).map( item => parseInt( item.prop( "id" ).match( /\d+/g ), 10 ) ) );

给定的错误是:

issue.prop is not a function.

所以我尝试了另一种:

let abc = Math.max.apply( null, jQuery( "#wrapper .item" ).map( issue => parseInt( jQuery(issue).prop( "id" ).match( /\d+/g ), 10 ) ) );

这给了我这个错误:

Cannot read property 'match' of undefined

我在这里做错了什么?

最佳答案

jQuery 的 map()函数分别将 Integer 索引和 Element domElement 传递给回调。

$(function() {
    let abc = Math.max.apply(null, $('#wrapper .item').map( (index, item) => parseInt( $(item).prop('id').match( /\d+/g ), 10 ) ) );

    console.log(abc);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="wrapper">
    <div id="item1" class="item">Item 1</div>
    <div id="item2" class="item">Item 2</div>
    <div id="item3" class="item">Item 3</div>
</div>

关于javascript - 将 jQuery map 函数转换为箭头函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59339061/

相关文章:

javascript - 可以包含 javascript 的 HTML 属性

javascript - 是否可以在 JavaScript 函数内从调用者的作用域访问变量?

javascript - 使用 jquery 在特定 DIV 中滚动事件

javascript - 动态创建的表 - 一起成长

javascript - 无法在ajax的帮助下使用选择选项更新数量

javascript - 如何在提交按钮后从输入中删除文件

javascript - 如果存在不同的类,我如何有条件地将 CSS 应用于类?

javascript - plupload removeFiles

javascript - 使用 nvd3.js 库将 '%' 符号添加到 y 轴上的所有刻度而不乘以 100

javascript - ajax jquery 表单停止发送电子邮件