To use the optional index argument in the .map() method

I’m trying to figure out this problem:

  • Use the optional arguments in an iterator to include the index or the entire array.

I want to use the optional index argument in the .map() method. Here’s what I have so far:

const sampleArray = [3,4,6,7,8];

const ArrayChain = sampleArray.map(number => number * 2);

console.log(ArrayChain);  // logs [ 6, 8, 12, 14, 16 ]

My problem is that I can’t figure out the syntax for using the optional arguments.

Here’s what the MDN documentation says:

let new_array = arr.map(function callback( currentValue[, index[, array]]) {
// return element for new_array
}[, thisArg])

I’ve tried several variations to emulate that syntax, but they all fail. If I wanted to use the index argument in my code I have above, how do I format the syntax? Can someone give me an example of what my code would have to look like in order to run with the index argument of the .map() method?

We can use .map() to enumerate an array…

a = [13, 21, 34, 55, 89]
b = a.map((x, i) => [i, x])
console.log(b)
[ [ 0, 13 ], [ 1, 21 ], [ 2, 34 ], [ 3, 55 ], [ 4, 89 ] ]

Notice how the index is the second positional argument?

We may find ourselves in a situation where this will be helpful such as an undo after an array sort.

1 Like

I see…applying that to my code, it would look like:

const sampleArray = [3,4,6,7,8];
const ArrayChain = sampleArray.map((number, index) => [index, number * 2]);

And I see that it returns the index number in the array that comes back.

The thing that gets me is that I don’t know how I would have ever constructed that based on the syntax in the documentation. It seems like the syntax that works is completely different from what’s listed. Is it just because we’re using the arrow function expression?

1 Like

In the documentation, the square brackets indicate optional parameters. They are not part of the syntax.

1 Like

Ohhh, okay. Duh. That makes a lot more sense now, thanks.

2 Likes

You’re welcome. Nobody seems too keen to show a use case for the optional array argument, or the thisArg. Dig around and see if you can come up with some examples for this method before you dart off to another iterator. What you learn may well apply to others as well so will be time well spent.

Some months ago I remember studying forEach and devised a use case for all its parameters but for the love of me cannot find it. I thought it was a marvelous example, but then one would, wouldn’t one?

Too bad that I write so much tiny code it’s difficult to remember most of it. Lesson learned for someone, one hopes.

2 Likes

Found it…

upper = []
lower = ['a', 'b', 'c', 'd', 'e', 'f']
lower.forEach(function(x) { 
    this.push(x.toUpperCase())
}, upper)
console.log(upper)

upper = []
lower.forEach(function(x, i) { 
    this[i] = x.toUpperCase()
}, upper)
console.log(upper)

Not a very good example but the mechanics are there.

Thanks, Roy. I’m giving myself a break for the weekend but I’ll study it on Monday and see if it makes sense. I’m still very much a beginner and haven’t been able to think of use cases for the optional arguments yet, but this conversation is putting it on my radar.

1 Like