How to catch last iteration inside $.each in jQuery?

前端 未结 6 473
野的像风
野的像风 2021-01-03 22:58
var arr = {\'a\':fn1,\'b\':fn2,\'c\':fn3}

$.each(arr,function(name,func){
(do something particular for the last iteration)
...
})

It\'ll be best i

6条回答
  •  庸人自扰
    2021-01-03 22:58

    Now that I have seen your duplicate question - where you state, "For the following,it's 'c':fn3" - it seems you might be after the value of the maximum property of an object.

    var obj = { 'a': fn1, 'b': fn2, 'c': fn3 };
    
    var maxKey;
    for (var key in arr) {
        if (!(maxKey > key)) {
            maxKey = key;
        }
    }
    
    // fn will be fn3
    var fn = obj[maxKey];
    

提交回复
热议问题