'How to reverse and make a string from the array in JavaScript with comma separated and space array?

My array is : var array1 = ['c', 'a', 'k', 'e', ' ', 'e', 'a', 't', ' ', 'I'];

Output expected is : I eat cake

How I can do that using JavaScript ?



Solution 1:[1]

You can use join to concate all of the chars in an array, then use split to separate them by " ". now, you can reverse that array of strings and join them once again by " ".

var s = ['c','a','k','e',' ', 'e','a','t',' ','I'];

var str = s.join("").split(" ").reverse().join(" ");
console.log(str);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ran Turner