2021年4月29日星期四

Strange behavior when JavaScript sorts array elements with .map()

I´m using this code:

const sortedPlayers = props.scorer.sort((a, b) => {    if (a.nummer > b.nummer) {      return 1;    }    if (a.nummer < b.nummer) {      return -1;    }    return 0;  });               {sortedPlayers.map((player) => {})}  

And it works as intended, but the strange thing is that this also works:

const sortedPlayers = props.scorer.sort((a, b) => {}  {props.scorer.map((player) => {})}  

Can you please explain this mystery?

Thanks in advance /Peter

https://stackoverflow.com/questions/67326904/strange-behavior-when-javascript-sorts-array-elements-with-map April 30, 2021 at 09:01AM

没有评论:

发表评论