javascript - 了解 javascript 中 forEach 函数语句中的语法
问题描述
我现在正在使用 forEach 语句,这里有一段代码可以成功执行我正在处理的内容,但我不理解一个特定的语句。这段代码不是我写的。
let test = [10, 12, 14, 15, 16, 18];
test.forEach(function(num, index, array) {
if (num % 3 === 0) {
array[index] = num += 100; // <- This is the line of code that I am confounded by
}
});
console.log(test);
我只是不明白它背后的逻辑。
抱歉,如果问题措辞不佳,这是我发布的第一个与编码相关的问题,感谢您的帮助。
解决方案
这是您正在寻找的解决方案:
我array[index] = num += 100;
不觉得很奇怪。
首先num += 100
是在当前数字上加 100,最后将其分配给array[index]
以下是您的代码的简化版本
let test = [10, 12, 14, 15, 16, 18];
test.forEach(function(num, index, array) {
if (num % 3 === 0) {
num = num + 100; // Adding 100 to old value (identical to num += 100)
array[index] = num; // I do't think this is a weird code now
}
});
console.log(test);
希望这会有所帮助谢谢......
推荐阅读
- linux - 在调用 NASM 中的函数之前,%rsp 是否应该与 16 字节边界对齐?
- php - 根据当前帖子类型搜索不同的按钮
- google-maps - Flutter - 获取粗略的位置以改善搜索结果 - 无需明确权限
- angular - HeaderComponent.html:313 错误类型错误:_co.logout 不是函数
- c# - 尝试为 MenuStrip 着色,左侧出现白色边框
- python-3.x - Python3 pika channel.basic_consume() 导致 MySQL 连接过多
- c++ - 独特的科学软件在 Windows 10 上更新后出现 0xc00007b 错误
- paypal - 在 PayPal 中自动捕获订单
- python - 如何在列中对齐两个熊猫数据框的值?
- azure-data-factory - Azure 数据工厂:使用 Azure 数据工厂活动从 CSV 文件中展平/规范化 cloumn