I have one array as below
var arr = ["john doe first", "robert way", "jason doe", "alex"];
Now i want to convert this array like below.
var result = ["john", "doe", "first", "robert", "way", "jason",
"doe", "alex"];
Quick explanation is i need convert each array element of string into array and reset it into the array. Array index is not important. I tried arr.split(" ") even arr.toString() and then arr.split(" "). I don't know what i'm missing here.
You can simply use Array#join
and String#split
:
var arr = ["john doe first", "robert way", "jason doe", "alex"];
console.log(arr.join(' ').split(' '));
If your Strings have more than one space separating your words :
Add .filter(Boolean)
to remove the empty Strings
var arr = ["john doe first", "robert way", "jason doe", "alex"];
console.log(arr.join(' ').split(' ').filter(Boolean));
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments