Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get current index in Array prototype map?

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.querySelectorAll('li.g'), function(e) {
         return {
             rectangle: e.getBoundingClientRect();
         }
    }
}

However, I also want to store the order in which this elements appear in the DOM, and I don't know how to do that.

I know that I'm storing this in an array, and the order would be the index of the array. For example:

var listings = getListings();
console.log(listings[0]); // rank #1
console.log(listings[1]); // rank #2
// etc...

but I'm inserting the json object in a database, and the easiest way to store the "rank" information is by creating a property "rank" in my object, but I don't know how to get the "index" of the current array.

Something like:

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

Any help pointing me to the right direction will be greatly appreciated! Thanks

like image 275
ILikeTacos Avatar asked Nov 15 '13 14:11

ILikeTacos


People also ask

What is the difference between index and arrayobj in map () function?

index: The index is an optional argument map () which is an array index of provided current element. arrayobj: The arrayobj is an optional argument in map () which is the array object where the current element belongs. Below examples illustrate the Index inside map () function: Example 1:

How do you map an array in JavaScript?

In JavaScript, map () method handles array elements, which creates a new array with the help of results obtained from calling function for each and every array element in an array. The index is used inside map () method to state the position of each element in an array, but it doesn’t change the original array.

What is index inside map () method in JavaScript?

The index is used inside map () method to state the position of each element in an array, but it doesn’t change the original array. Parameters: The Index inside function accepts three parameters as mentioned above and described below:

What does the index of an array mean?

2) index The index of the current element being processed in the array. 3) array The array map was called upon. Share Improve this answer Follow edited Sep 25 '19 at 11:59


1 Answers

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
         }
    }
}
like image 174
Tibos Avatar answered Sep 27 '22 20:09

Tibos