首页 > 解决方案 > 如何对数组中的每个对象执行计算,然后输出该数组?

问题描述

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

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

标签: javascriptarraysrsa

解决方案


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

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

// should be [2, 4, 6]
console.log(newArr); 


推荐阅读