0

I would like to know how to replace the accented characters and symbols in a object using javascript.

I have tried the below method but I would like to know if there is alternative way to do,

var result = obj.countries.forEach(e=>replaceData(obj));
function replaceData(obj){
    var in_chrs   = 'àáâãäçèéêëìíîïñòóôõöùúûüýÿÀÁÂÃÄÇÈÉÊËÌÍÎÏÑÒÓÔÕÖÙÚÛÜÝ',
      out_chrs  = 'aaaaaceeeeiiiinooooouuuuyyAAAAACEEEEIIIINOOOOOUUUUY', 
      chars_rgx = new RegExp('[' + in_chrs + ']', 'g'),
      transl    = {}, i,
      lookup    = function (m) { return transl[m] || m; };

  for (i=0; i<in_chrs.length; i++) {
    transl[ in_chrs[i] ] = out_chrs[i];
  }

  return s.replace(chars_rgx, lookup);
}

var obj ={
 "contries":{
   "CI": "côte-d’ivoire",
   "RE": "réunion",
   "AG": "antigua & barbuda",
   "SG": "singapore",
   "TH": "thailand",
   "US": "united-states"
 }
}


Expected Output:

{
 "contries":{
   "CI": "cote divoire",
   "RE": "reunion",
   "AG": "antigua and barbuda",
   "SG": "singapore",
   "TH": "thailand",
   "US": "united states"
 }
}

Senthil
  • 961
  • 1
  • 8
  • 21
  • 1
    There is a similar question below. Check if you find a better way here. https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript – koushikmln Aug 25 '19 at 14:53
  • 1
    Possible duplicate of [Remove accents/diacritics in a string in JavaScript](https://stackoverflow.com/questions/990904/remove-accents-diacritics-in-a-string-in-javascript) – Dez Aug 25 '19 at 15:03

0 Answers0