javascript - array.map() - currentValue and index parameters switched?
问题描述
I am pulling an input elements array from the DOM using the the id and using .map() to loop through the array. My code works, however currentValue and index are switched:
ports = $("#edit_display_control_ports > li > input").map(function(currentValue, index){
return index.value;}
).toArray();
Again, this code works. According to documentation, the first parameter of the map() function should be the currentValue and the second, index. However I have to switch the two. The currentValue return the index of the array, while the index parameter returns the element.
解决方案
您部分正确,您的问题是您没有运行 javascript 的 native .map
,这是 jQuerys .map
,因为您在 jQuery 集合上调用它。
jQuery 的地图工作方式不同:
回调类型:Function(Object elementOfArray, Integer indexInArray) => Object 处理每个项目的函数。函数的第一个参数是数组项,第二个参数是数组中的索引 函数可以返回任何值。返回的数组将被展平为结果数组。在函数中,this 指的是全局(窗口)对象。
看看这里
推荐阅读
- image - 在 Outsystems Mobile 中显示联系人列表图像
- javascript - 如何使用 chart.js 自定义圆环图
- java - 将数据读入 ArrayList
- javascript - 当输入中的值已更改jQuery时更改另一个值字段
- haskell - 为什么 MLF 中有弹性和刚性边界?
- git - Git 是否会考虑时间来避免分支“竞赛”?还是合并顺序很重要?
- r - 在 R H2O 数据框中用 \N 替换 NULL
- android - Gradle 同步失败:找不到方法 GradleBuild.getIncludedBuilds()
- mysql - JSON 字符串 - 在 MySQL 数据库中包含单引号
- python-3.x - Pandas 数据框中的模式匹配