$.each() index start number in jQuery

前端 未结 8 1897
慢半拍i
慢半拍i 2021-01-05 02:28

How do I start jQuery $.each() index from 1 instead of 0?

I am using .each function to populate select box. So here I want to populate options in the se

相关标签:
8条回答
  • 2021-01-05 03:03
    $('.article_number').each(function(index) {
        index=index+1;
        $(this).text(index);
    });
    

    even starting from 0 each time it takes the index and adds one for the text.

    so 0 will become 1 in text. 1 will become 2 in text and so on.

    This won't interrupt the last item in line cause it's only changing the index of the text not on the each function.

    Hope this is what you ment.

    0 讨论(0)
  • 2021-01-05 03:06

    Not possible. jQuery.each loops over Objects (Arrays). For real objects, it uses a for in loop. Objectpropertys don't have a guaranteed index at all by the way.

    For Arrays it uses standard for loop, but you don't have access to the starting index.

    Your best shot is to just skip the first element.

    $.each(obj, function(index, elem) {
        if( index === 0 )
            return true;
    
        // code
    });
    
    0 讨论(0)
提交回复
热议问题