How to get the index value while using ko.utils.arrayForEach

How to get the index value while using ko.utils.arrayForEach

Asked on January 2, 2019 in knockout js.
Add Comment


  • 2 Answer(s)

        This is tough to get the index value.

        But doing this in manual is possible.

    for (var i = 0, j = comments.length; i < j; i++) {
        // use an anonymous function to keep the same code structure
        (function(comment, i) {
            tmp.push(comment);
            // do what you need with i here
        })(comments[i], i);
    }
    

        This code is used within the ko.utils.arrayForEach . Once Knockout will be released then the migration is easy .

    Answered on January 2, 2019.
    Add Comment

        The Index is transferred to all array functions as the 2nd argument:

    ko.utils.arrayForEach(items, function(item, index) {
        /* ... */
    });
    
    Answered on January 2, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.