I have two array
array number 1:
let search = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];
array number 2:
let replace = ['1', '2', '3', '4', '5','6', '7', '8', '9', '10','20', '30', '40', '50', '60','70', '80', '90', '100', '200','300', '400', '500', '600', '700','800', '900', '1000'];
and this is an input :
let word = "محمد";
I want in javascript search the letters of the variable word and replace them with the equivalent of number in the replace array .The replace array and the search array have the same numbers of letters and number.
The output will be:
م 40
ح 8
م 40
د 4
total = 92
let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];
if (word === "") {
document.write('the field is empty');
} else {
w = word.split("").filter(function(ele) {
return ele.replace(search, replace);
}).map(function(ele) {
return ele + ele;
});
}
console.log(w);
Assuming you meant the expected result was 92, you can do this by forst splitting the input, then finding the index of the item in find and aggregating the result using reduce finding the value from replace
let search = ['ا', 'ب', 'ج', 'د', 'ه','و', 'ز', 'ح', 'ط', 'ي','ك', 'ل', 'م', 'ن', 'س','ع', 'ف', 'ص', 'ق', 'ر','ش', 'ت', 'ث', 'خ', 'ذ','ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5','6', '7', '8', '9', '10','20', '30', '40', '50', '60','70', '80', '90', '100', '200','300', '400', '500', '600', '700','800', '900', '1000'];
let word = "محمد";
let result = word.split("")
.map(x => search.indexOf(x))
.reduce( (acc,i) => acc + parseInt(replace[i],10),0);
console.log(result);
You could impove this by storing integers in your replace array which would negate the need for parseInt
I would suggest creating a map for the two arrays:
let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];
let map = Object.fromEntries(search.map((letter, i) => [letter, replace[i]]));
let w = Array.from(word, letter => map[letter]).filter(Boolean);
console.log(w);
// If you want to have a concatenated string:
console.log(w.join(","));
// Or a sum:
console.log(w.reduce((a, b) => a + +b, 0));
Note: If your intention is to sum, or make other numerical calculations, I would suggest to make the replace array an array of numbers, not strings.
Logic
search.replace array.Working Fiddle
let word = "محمد";
let search = ['ا', 'ب', 'ج', 'د', 'ه', 'و', 'ز', 'ح', 'ط', 'ي', 'ك', 'ل', 'م', 'ن', 'س', 'ع', 'ف', 'ص', 'ق', 'ر', 'ش', 'ت', 'ث', 'خ', 'ذ', 'ظ', 'ض', 'غ'];
let replace = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '20', '30', '40', '50', '60', '70', '80', '90', '100', '200', '300', '400', '500', '600', '700', '800', '900', '1000'];
let sum = 0;
word.split("").forEach(letter => {
const index = replace[search.indexOf(letter)];
console.log(`${index} ${letter}`)
sum += +index;
});
console.log(`total = ${sum}`)