https://cypress.io logo
Cypress Map Function
# i-need-help
a
Hi, I am new to Cypress but not in JS. I was using the map function in cypress and found out that the map for Cypress takes 2 arguments which is same as JS, but the first argument in Cypress is index and 2nd one is Element. But it's the reverse in JS. Can someone please explain why is that so? -------------------------------------------------------------------------------------------------------------------------------------- Cypress(taken from the cypress docs):- map((index, element) => { cy.log(el) }) --------------------------------------------------------------------------------------------------------------------------------------- JS(take from MDN docs):- map((element, index) => { /* … */ })
g
Can I ask where in Cypress docs you found
cy.map
documentation 🙂
sorry for late reply, just saw your messae 🙂
d
this example shows jquery map
a
yes, if you see jquery docs it also has the first argument as a value and the second as a key(index). See here https://api.jquery.com/jquery.map/
g
Yup, this is jQuery object yielded by Cypress querying commands. JQuery puts the index first. You want to use normal array
.map
, convert the jQuery to array using ...
jQuery.toArray
Something like
should($list => $list.toArray().map(el => ...
a
This is taken from Jquery docs, and it's saying that the first argument should be the value and not the index. Am I missing something here? 🤔
g
Well, who am I to argue with jQuery docs, but in my examples that are tested right here https://glebbahmutov.com/cypress-examples/recipes/first-column.html#rows-with-th-and-td-elements I used
$.map((k, element)
so ... you can check yourself, put
debugger
into the callback or print the arguments and you will see