How to convert Array of Strings with space into single Array list in Javascript?

parth

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.

Zenoo

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.

edited at
0

Comments

0 comments
Login to comment

Related

Convert array of Strings to Array list

How do I convert an array of strings into append-able list?

How to convert a DOM node list to an array in Javascript?

Convert array of strings to List<string>

Convert multidimensional array to list of single array

How to convert a list of strings into a numeric numpy array?

How to convert javascript array to specific list of objects

How to convert an array of strings to float in Javascript

Convert Array elements into single strings (PHP)

how to convert json to array in javascript for an li list

How to convert strings in a Pandas Dataframe to a list or an array of characters?

How can I convert a list of strings to an array of float numbers?

How to convert an entire list of strings to a normal array or list?

How to convert a deep multi dimensional array to a single dimensional array - Javascript

How to convert word list to array in JavaScript

Convert array of Strings to list of Integers?

How to convert Python list of strings into C array of wchar_t?

How to convert an array of list to a single list in node js

Convert array of strings into an array of objects in javascript?

How to convert different strings in python list to array in bash script

How to convert a python list of tuples to an array in JavaScript?

Mule 4 - Convert Array of strings to single string

How to convert list of enums into array of strings?

Convert nodes that consists of nested objects, strings, array into an unordered list (Javascript)

How to parse an array of objects to a list of strings? (Javascript)

Convert array of strings to a single integer

How to convert an object which can be array or single object to an array in javascript

Convert array to list in javascript

Convert list to array in JavaScript