我正在尝试将以下工作函数转换为箭头函数,但失败了。我不知道我做错了什么。
这是我的起始情况:
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/