Finds an array element of a set of strings arranged in different order from a set of arrays
When you convert a string to an array and then sort the arrays, ABCD and BDCA use sort to become ABCD, and the sequenced string as the object's key to hold the sorted string.
By converting characters to Unicode encoding, the sort used is implicitly converted.
I'm just here to provide a simple idea, so the example is a simple one-dimensional array, if it is a two-dimensional array, use recursion.
var stringclassify = function () { var arrlength = arr.length, obj = {}, i = 0, num, item, name, Strarr, new STR; for (; i < arrlength; i++) { item = Arr[i]; Strarr = Arr[i].split ("); Strarr.sort (); Newstr = Strarr.join ("); if (!obj[newstr]) { OBJ[NEWSTR] = []; } Obj[newstr].push (item); } Console.log (obj); for (name in obj) { console.log (obj[name]); } }; var arr = [' abcd ', ' hello ', ' bdca ', ' Olleh ', ' cadb ', ' NBA ', ' ABN ', ' abc ']; Stringclassify (arr);
JS Array operation-Find an array element of a set of strings arranged in different order