I have to solve this problem below. The output should be an object map with keys as the sorted value and all other variations as an array assigned to the sorted key. I'm able to generate the values as strings but not as an array with all values. Any pointers?
var list = ['ab', 'cd', 'acb', 'dc', 'ba', 'abc', 'cba', 'bca'];
var oMap = {};
var pairs = [];
for(var i=0; i <list.length; i++){
var temp = sortstr(list[i]);
oMap[temp] = {};
if(temp in oMap){
if(temp === sortstr(list[i])){
oMap[temp] = temp + "," + list[i]; --> should store in array
}
}
else {
oMap[temp] = {};
oMap[temp] = list[i];
}
}
console.log(oMap);
function sortstr(text) {
return text.split('').sort().join('');
};
Expected Output:
{ab: [ab,ba], cd: [cd,dc], abc: [abc,bca,acb,cba] }
Actual Output:
{ab: "ab,ba", cd: "cd,dc", abc: "abc,bca"}
array oMap[temp] = temp + "," + list[i];like sooMap[temp] = pairs.push(list[i]);