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']]
-
2what is your expected output?Uthistran Selvaraj– Uthistran Selvaraj2020年01月08日 20:25:54 +00:00Commented Jan 8, 2020 at 20:25
-
@UthistranSelvaraj i have given in the bottomPremjeet Kumar– Premjeet Kumar2020年01月08日 20:27:46 +00:00Commented Jan 8, 2020 at 20:27
3 Answers 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!
1 Comment
You could map the joined values.
var array = [['ba', 'ab'], ['bab', 'abb']],
result = array.map(a => a.join(', '));
console.log(result);
Comments
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);