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

前端 未结 6 470
野的像风
野的像风 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 23:04

    Philippe Leybaert's answer outlines the problems with your question very well, and there is probably a clearer way of doing what you want. But that said, I cannot see a way to do what you ask without using an extra variable.

    var obj = { 'a': fn1, 'b': fn2, 'c': fn3 };
    var lastKey;
    
    $.each(obj, function(key, fn) {
        // do stuff...
        lastKey = key;
    });
    
    obj[lastKey].doStuffForLastIteration();
    

提交回复
热议问题