如何对数组中的每个对象执行计算,然后输出该数组?

Ola*_*ner 6 javascript arrays rsa

我正在尝试使用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;
}

Run Code Online (Sandbox Code Playgroud)

我希望得到一个修改后的整数(密文)数组。谢谢

Dan*_*one 6

您可以在数组上使用Javascript的map()函数。

const arr = [1, 2, 3];
const newArr = arr.map(i => i * 2);

// should be [2, 4, 6]
console.log(newArr); 
Run Code Online (Sandbox Code Playgroud)


Gui*_*mmi 2

使用 Javascript地图功能,如下所示:

function encryptText() {
  var plaintext = document.getElementById('plaintext').value;
  var n = letterValue(String(plaintext));
  ciphertext = addon_array.map((el) => Math.pow(el, e) % n);
  document.getElementById("output3").innerHTML = "Encrypted text = " + ciphertext;
}
Run Code Online (Sandbox Code Playgroud)