Removing accents in an array of strings

I have an array of strings like

let array = ['Enflure', 'Énorme', 'Zimbabwe', 'Éthiopie', 'Mongolie']

I want to sort it alphabetically, so I use array.sort()and the result is:

['Enflure', 'Mongolie', 'Zimbabwe', 'Énorme', 'Éthiopie']

I think the emphasis here is the problem, so I would like to replace it with Éthe Ewhole array.

I tried this

for (var i = 0; i < (array.length); i++) {
    array[i].replace(/É/g, "E");
}

But that did not work. How can i do this?

+4
source share
2 answers

You can use String#localeCompare.

The method returns a number indicating whether the link string will be before or after or will be the same as the specified string in the sort order. localeCompare()

locales options , . , locales options, .

var array = ['Enflure', 'Mongolie', 'Zimbabwe', 'Énorme', 'Éthiopie'];

array.sort((a, b) => a.localeCompare(b));

console.log(array);
Hide result
+7

JS . , replace , :

for (var i = 0; i <(array.length); i++) {
 array[i]=array[i].replace(/É/g,"E");
}

:

array=array.map(s=>s.replace(/É/g,"E"));
+5

Source: https://habr.com/ru/post/1682601/


All Articles