How to use the map function in this?

Tell us what’s happening:
Describe your issue in detail here.

if let a = [45,20,64,87,96,54];

/* suppose I want to add a[i] + a[i+1] = b[i] which

  • is equal to (example “i=0”) a[0] + a[1] = b[0];
    *how to do that with the function map instead of a for loop?
    *because in the function map I don’t have a “i” variable.

const b = a.map(function(a) {
return
});

console.log(b);





**Your browser information:**

User Agent is: <code>Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.69 Safari/537.36</code>
because in the function map I don’t have a “i” variable.

The map method does have an optional index variable and that’s the index of the current element being processed in the array.

To know more, you can have a look here: Array.prototype.map() - JavaScript | MDN

1 Like

It also passes in a reference to the array itself as a third parameter, so you don’t have to refer to an out of scope reference.

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.