Replace letters with numbers in js

I need to change all the letters in the string according to the pattern from the array. I started, but I couldn't continue. There was an idea to search through all the letters to get, but then the speed of work would leave much to be desired. In this code, I create a variable in the array, in which a new string will be written:

array.forEach(function(item, i, arr) {
        arr[i]["__TMP__"] =
            stringTable.indexOf( item["name"].toString().charAt(0).toUpperCase() );
    });

Is there any way to do this with replace?

Template array:

stringTable: string[] = [
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z"
];

Input string: Tim outout string: 019008012 (019- t, 008- i, 012- m)

Author: Nikolay, 2017-08-07

4 answers

Instead of an array, it is better to use an object, in this case you will not need to search, for example

{
  "A": "000",
  "B": "001",
  "C": "002",
  "D": "003",
  "E": "004",
  "F": "005",
  "G": "006",
  "H": "007",
  "I": "008",
  "J": "009",
  "K": "010",
  "L": "011",
  "M": "012",
  "N": "013",
  "O": "014",
  "P": "015",
  "Q": "016",
  "R": "017",
  "S": "018",
  "T": "019",
  "U": "020",
  "V": "021",
  "W": "022",
  "X": "023",
  "Y": "024",
  "Z": "025"
}

Getting the desired string to replace will be reduced to simply getting the property:

symbolsMap[char.toUpperCase()];

This code can be used in the function passed to the method replace

var symbolsMap = {
  "A": "000",
  "B": "001",
  "C": "002",
  "D": "003",
  "E": "004",
  "F": "005",
  "G": "006",
  "H": "007",
  "I": "008",
  "J": "009",
  "K": "010",
  "L": "011",
  "M": "012",
  "N": "013",
  "O": "014",
  "P": "015",
  "Q": "016",
  "R": "017",
  "S": "018",
  "T": "019",
  "U": "020",
  "V": "021",
  "W": "022",
  "X": "023",
  "Y": "024",
  "Z": "025",
  
  "Р": "-11",
  "Ы": "-21",
  "К": "-31"
}

var str = "Tim";
var numbers = str.replace(/./gi,$0=>symbolsMap[$0.toUpperCase()]||$0);
console.log(numbers);

var str = "рык";
var numbers = str.replace(/./gi,$0=>symbolsMap[$0.toUpperCase()]||$0);
console.log(numbers);
 3
Author: Grundy, 2017-08-08 07:29:29

If you understand the question correctly, you can try this.

function pad(num, size) {
    var s = num+"";
    while (s.length < size) s = "0" + s;
    return s;
}

let input = 'Tim'
let inputLower = input.toLowerCase();
let output = '';
for (let i = 0; i < inputLower.length; i++) {
    output += pad(inputLower[i].charCodeAt(0) - 97, 3);
}
console.log(output);
 1
Author: Nazar Kalytiuk, 2017-08-07 20:43:21

Here is the code that came out, we do not use an array and charAt + catch errors if the user entered non-English letters.

document.querySelector("button").onclick = function() {
  var inputText = (document.querySelector(".text-in").value).toLowerCase();
  var outText = "";
  if (inputText != "") {
    inputText = inputText.split('');
    for (var i = 0, inputTextL = inputText.length; i < inputTextL; i++) {
      var num = (inputText[i].charCodeAt(0)) - 96;
      if (num < 1 || num > 26) {
        document.querySelector(".text-in").value = "English word, please!";
        return;
      }
      if (num < 9) {
        outText = outText + "00" + num;
      } else {
        outText = outText + "0" + num;
      }
    }
    document.querySelector(".text-in").value = outText;
  }
}
<input type="text" class="text-in">
<button>Зашифровать</button>
 1
Author: Daniil Dubchenko, 2017-08-07 21:19:25

String.prototype.replaceAll = function(replacement) {
  var _this = this;

  replacement.forEach(function(element, index) {
    var re = new RegExp(element, "gi");

    _this = _this.replace(re, "000".substring(0, "000".length - index.toString().length) + index.toString());
  });

  return _this;
};

var originalString = "Hello Привет 007!!!";
var newString = originalString.replaceAll(['h', 'e', 'l', 'п', 'р', 'и', '!', 'в', 'е', 'т', 'o']);

console.log(originalString + " -> " + newString);

For this code, it is important that there are no numbers among the characters to replace ['h', 'e', 'l', 'п', 'р', 'и', '!', 'в', 'е', 'т', 'o']. If there are numbers , please write a more detailed question with all the conditions. The decision will depend on it.

P.S.: If there are numbers: you can first replace the numbers with a regular expression, and then replace the other characters. Because if we first replace the characters in the string, and then start replacing the numbers , more numbers will appear in the string...

 1
Author: Alexander Bragin, 2017-08-08 09:08:43