Skip to content

Instantly share code, notes, and snippets.

@FreePhoenix888
Last active October 4, 2020 06:47
Show Gist options
  • Save FreePhoenix888/4fa8c4e4e21709a6b3412a7c9c4e00f1 to your computer and use it in GitHub Desktop.
Save FreePhoenix888/4fa8c4e4e21709a6b3412a7c9c4e00f1 to your computer and use it in GitHub Desktop.
function aclean(arr) {
let map = new Map();
for (let word of arr) {
// разбиваем слово на буквы, сортируем и объединяем снова в строку
let sorted = word.toLowerCase().split("").sort().join(""); // (*)
map.set(sorted, word);
}
return Array.from(map.values());
}
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
alert( aclean(arr) );
/* For objects
function aclean(arr) {
let obj = {};
for (let i = 0; i < arr.length; i++) {
let sorted = arr[i].toLowerCase().split("").sort().join("");
obj[sorted] = arr[i];
}
return Object.values(obj);
}
let arr = ["nap", "teachers", "cheaters", "PAN", "ear", "era", "hectares"];
alert( aclean(arr) );*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment