0

I have a function which groups anagrams together

function groupAnagrams(strs) {
  let result = {};
  for (let word of strs) {
    let cleansed = word.split("").sort().join("");
    if (result[cleansed]) {
      result[cleansed].push(word);
    } else {
      result[cleansed] = [word];
    }
  }
  console.log(Object.values(result));
  return Object.values(result);
}

it prints the results in the following format

[ [ 'abc', 'bac', 'cba' ], [ 'fun', 'fun', 'unf' ], [ 'hello' ] ]

However I would like the output to look like the following

abc, bac, cba

fun, fun, unf

hello

How can I achieve this?

3 Answers 3

3

you can do something like this

const data = [ [ 'abc', 'bac', 'cba' ], [ 'fun', 'fun', 'unf' ], [ 'hello' ] ]

data.forEach(row => console.log(row.join(', ')))
//or

console.log(data.map(row => row.join(', ')).join('\n'))

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

Comments

1

Since it's a node.js-tagged question I'll give an example with os.EOL

const { EOL } = require('os');
const lines = [ [ 'abc', 'bac', 'cba' ], [ 'fun', 'fun', 'unf' ], [ 'hello' ] ];

const output = lines.map((words) => words.join(', ')).join(EOL);

process.stdout.write(output);

Comments

0

Here's another solutions...

function groupAnagram(arr){
  arr.map((item) => {
    console.log(`${item.join(', ')} \n\n`)
  });
}

groupAnagram([ [ 'abc', 'bac', 'cba' ], [ 'fun', 'fun', 'unf' ], [ 'hello' ] ]
);

Comments

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.