0

I am trying to convert an array of array's each array into a string.

I know the method flat where all the array of the array becomes a single array but this is not solving my full purpose.

array = [['ba','ab'],['bab','abb']]

my tried code is:

let merged = array.map(a => {
  console.log(a)
  a.reduce((a, b) => a.concat(b), []);
})

console.log(merged);

Expected output is: [['ba,ab'],['bab, abb']]

2
  • 2
    what is your expected output? Commented Jan 8, 2020 at 20:25
  • @UthistranSelvaraj i have given in the bottom Commented Jan 8, 2020 at 20:27

3 Answers 3

3

You can use Array.prototype.join() for this purpose. From the documentation:

The join() method creates and returns a new string by concatenating all of the elements in an array (or an array-like object), separated by commas or a specified separator string. If the array has only one item, then that item will be returned without using the separator.

Like the following:

const array = [['ba', 'ab'], ['bab', 'abb']];
const result = array.map(e => e.join(','));
console.log(result);

Hope that helps!

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

1 Comment

i posted wrong output can you tell me how to get this output ?
1

You could map the joined values.

var array = [['ba', 'ab'], ['bab', 'abb']],
    result = array.map(a => a.join(', '));

console.log(result);

Comments

1

With respect to your comment on @norbitrial answer. Plus a little JS type conversion hack (just for education, you'd better use join(",")). And I think you should accept his answer.

const array = [['ba', 'ab'], ['bab', 'abb']];
const result = array.map(e => ["" + e]);
console.log(result);

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.