0

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"}
2
  • 1
    The posted code makes no apparent attempt to make arrays. What exactly is the problem? Commented Jun 15, 2019 at 14:41
  • getting all the values in the array if I try to make this line to store in an array oMap[temp] = temp + "," + list[i]; like so oMap[temp] = pairs.push(list[i]); Commented Jun 15, 2019 at 14:45

2 Answers 2

2

There are two lines in below code added by me.

oMap[temp] = oMap[temp] || [];

It will check if temp property already exists on oMap then keep its value same otherwise make it empty array.

oMap[temp].push(list[i]);

It will add the new value to end of the array which is at temp in oMap

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] = oMap[temp] || [];
   oMap[temp].push(list[i]);
}

console.log(oMap);

function sortstr(text) {
    return text.split('').sort().join('');
};

You are building a single value from the array so if you want higher order functions you can use reduce()

var list = ['ab', 'cd', 'acb', 'dc', 'ba', 'abc', 'cba', 'bca'];


const res = list.reduce((ac,a) => {
  let k = sortstr(a);
  ac[k] = (ac[k] || []).concat(a);
  return ac;
},{})

console.log(res)


function sortstr(text) {
    return text.split('').sort().join('');
};

Sign up to request clarification or add additional context in comments.

Comments

2

You need an array as target for the values and omit some check and unnecessary assignments.

function sortstr(text) {
    return text.split('').sort().join('');
}

var list = ['ab', 'cd', 'acb', 'dc', 'ba', 'abc', 'cba', 'bca'],
    oMap = {},
    temp, i;

for (i = 0; i < list.length; i++) {
    temp = sortstr(list[i]);
    if (temp in oMap) {
        oMap[temp].push(list[i]);
    } else {
        oMap[temp] = [list[i]];
    }
}

console.log(oMap);
.as-console-wrapper { max-height: 100% !important; top: 0; }

An even shorter approach by using a default value for falsy content.

function sortstr(text) {
    return text.split('').sort().join('');
}

var list = ['ab', 'cd', 'acb', 'dc', 'ba', 'abc', 'cba', 'bca'],
    oMap = {},
    temp, i;

for (i = 0; i < list.length; i++) {
    temp = sortstr(list[i]);
    oMap[temp] = oMap[temp] || []; // check if truthy or take array
    oMap[temp].push(list[i]);
}

console.log(oMap);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2 Comments

wow great. I was doing a rookie mistake by assigning the values like so pairs.push(list[i]); oMap[temp] = pairs;
Im getting this error Uncaught TypeError: oMap[temp].push is not a function at app.js:9

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.