javascript - 如何对数组中的每个对象执行计算,然后输出该数组?
问题描述
我正在尝试在 Javascript (n^e mod n) 中对数组中的每个元素 e 执行计算,然后输出随后创建的新数组。我该怎么做?到目前为止,这是我想出的,但是代码不起作用。
到目前为止,这是我想出的,但是代码不起作用。
function encryptText() {
var plaintext = document.getElementById('plaintext').value;
var n = letterValue(String(plaintext));
ciphertext = array()
foreach(addon_array as key => col) {
ciphertext[key] = Math.pow(col, e) % n;
}
document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
我希望得到一个修改后的整数数组(密文)作为结果。谢谢
解决方案
您可以在数组上使用 Javascript 的map()函数。
const arr = [1, 2, 3];
const newArr = arr.map(i => i * 2);
// should be [2, 4, 6]
console.log(newArr);
推荐阅读
- python - 如何在 jupyter notebook 中修复这个内核错误
- spring-boot - Spring Boot:java.sq.SqlException:操作数应包含 1 列
- python-3.x - 获取作为回调或类似事件/中断处理程序传递的函数名称?
- xamarin - 使用 Xamarin 在我的 db Slite 中插入寄存器,错误
- node.js - 如何检索我的抽搐统计数据以显示在我的网站上?
- node.js - 如何从 LinkedIn API 一次请求多个范围?
- audio - 如何递归地计算 p 分位数?
- r - 使用 openxlsx::addStyle 显示为百分比,而无需单击 Excel 中的单元格
- c++ - 当 I/O 对象已经有执行器时,为什么需要 `net::dispatch`?
- html - 在 HtmlService 生成的 html 中包含响应 url