How to get current index in Array prototype map?

后端 未结 1 684
日久生厌
日久生厌 2020-12-10 04:51

I\'m using Array.prototype.map.call to store in an array a bunch of node list objects:

function getListings() {
    return Array.prototype.map.call(document.         


        
1条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-10 05:20

    The MDN documentation says:

    callback is invoked with three arguments: the value of the element, the index of the element, and the Array object being traversed.

    So

    function getListings() {
        return Array.prototype.map.call(document.querySelectorAll('li.g'), function(e, rank) { // magic 
             return {
                 rectangle: e.getBoundingClientRect(),
                 rank: rank // <-- magic happens
             }
        }
    }
    

    0 讨论(0)
提交回复
热议问题