1
I made the function below that removes accents from a string using values from an object:
function remAcentos(p){
var acc = {
// ↓ ↓ ↓ ↓ ↓
'á': 'a', 'â': 'a', 'à': 'a', 'ã': 'a', 'â': 'a',
// ↓ ↓
'é': 'e', 'ê': 'e',
'í': 'i',
// ↓ ↓ ↓
'ó': 'o', 'õ': 'o', 'ô': 'o',
'ú': 'u'
}
return p.replace(/[áàãâéêíóõôú]/g, function(m){
return acc[m];
});
}
console.log(remAcentos('brásíliaóúàô'));
She owns a replace
with a regex that matches accented letters and replaces the value of the item in the object and returns the string without accents.
It works perfect, but I’m intrigued with repetitions and a bit with the aesthetics of the code (see the indicative arrows in the code) and I think this can be optimized without repetitions.
How could I avoid such repetitions? I thought of something like:
var acc = {
'áâàãâ': 'a',
'éê': 'e',
'í': 'i',
'óôõ': 'o',
'ú': 'u'
}
But if I do it this way, how could I get the value of the item in the object acc
when the replace
marry a sharp letter?
See this: https://answall.com/a/54426/132
– Victor Stafusa
I’m sure it’s duplicate, but I didn’t find any good question to put as duplicate.
– Victor Stafusa
rsrs.. try to find it. Isac’s answer is good.
– Sam
I found some that were poorly formulated. But then I think it is not good to mark as duplicate.
– Victor Stafusa