What exactly is the difference between protractor map and protractor each function? The document says each and map both apply the callback function to ElementFinder from the respective ElementFinderArray. But how do they differ from each other? If they are one at the same thing, then why have a duplicate function be it map() or each()?
Asked
Active
Viewed 119 times
-1

yunzen
- 32,854
- 11
- 73
- 106

Deekshith Anand
- 2,175
- 1
- 21
- 24
-
map will create / return a new areay and each modifies the old array. At least for Vanilla js – Aaron Mar 27 '20 at 14:16
-
What does the documentation say? Any links? – yunzen Mar 27 '20 at 14:20
-
@yunzen [map](https://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.map) and [each](https://www.protractortest.org/#/api?view=ElementArrayFinder.prototype.each) from documentation – Deekshith Anand Mar 27 '20 at 14:25
-
each loops..... map returns a new data. Not the same thing. – epascarello Mar 27 '20 at 14:39
-
Why can't I get `map` to return an array of ElementFinders? :( – Ian Grainger Mar 31 '20 at 14:18
1 Answers
0
They both do the same and something different.
They both apply a function to each element of an array and they both return a promise. The promise is different though.
map
Returns
Type Description
!webdriver.promise.Promise A promise that resolves to an array of values returned by
. the map function.
each
Returns
Type Description
!webdriver.promise.Promise A promise that will resolve when the function has been
. called on all the ElementFinders. The promise will resolve to null.
For a description, what a map function does, compare MDN Array.prototype.map

yunzen
- 32,854
- 11
- 73
- 106