Reputation: 10033
how do I use Javascript to turn this list:
var array = ["no yes", "maybe certainly"];
into this one:
var array2 = ["no", "yes", "maybe", "certainly"]
Upvotes: 3
Views: 63
Reputation: 87203
It's better to use regex
here, if there are chances of multiple spaces in the array elements.
var array = [" no yes ", " maybe certainly "];
var array2 = array.join('').match(/\S+/g);
document.write(array2);
console.log(array2);
Upvotes: 4
Reputation: 77482
You can join
all elements in array, and after split
it to array, like so
// var array = ["no yes", "maybe certainly"];
var array = ["no yes ", " maybe certainly"];
var array2 = array.join('').trim().split(/\s+/g);
console.log(array2);
Upvotes: 1
Reputation: 193
var array = ["no yes", "maybe certainly"]
answer = converfunction(array);
function converfunction(array){
return array.toString().replace(',',' ' ).split(' ')
}
Upvotes: 0