Getting an array from a string with vanilla JS

Lets imagine that we have a comma-separated string of shoppingList items, and we want to convert it into an array.
let shoppingList = ‘soda, turkey sandwiches, potato chips, chocolate chip cookies’; Today, we’re going to look at how to do that, and a few considerations. Let’s dig in…
The String.split() method The String.split() method converts a string into an array by splitting it after a specific character (or characters).


This content originally appeared on Go Make Things and was authored by Go Make Things

Lets imagine that we have a comma-separated string of shoppingList items, and we want to convert it into an array.

let shoppingList = 'soda, turkey sandwiches, potato chips, chocolate chip cookies';

Today, we’re going to look at how to do that, and a few considerations. Let’s dig in…

The String.split() method

The String.split() method converts a string into an array by splitting it after a specific character (or characters). The first argument, the delimiter, the character or characters to split by.

We can use the String.split() method on it, passing in a comma (,) as the delimiter.

// returns ["soda", " turkey sandwiches", " potato chips", " chocolate chip cookies"]
let menu = shoppingList.split(',');

Removing leading white space

In menu, each item after soda in the list will have a leading space before it. To fix this, we can pass in a comma with a space as the delimiter instead.

// returns ["soda", "turkey sandwiches", "potato chips", "chocolate chip cookies"]
let menu2 = shoppingList.split(', ');

If you’re not sure if there will always be a space after the comma, you can pair this method with Array.map() and String.trim() to normalize the results.

// returns ["soda", "turkey sandwiches", "potato chips", "chocolate chip cookies"]
let menu3 = shoppingList.split(',').map(function (item) {
	return item.trim();
});

Stopping after a certain number of items

As an optional second argument, you can stop splitting your string after a certain number of delimiter matches have been found. The rest of the items are discarded.

// returns ["soda", "turkey sandwiches"]
let limitedMenu = shoppingList.split(', ', 2);

Here’s a demo with all of these examples.


This content originally appeared on Go Make Things and was authored by Go Make Things


Print Share Comment Cite Upload Translate Updates
APA

Go Make Things | Sciencx (2021-05-05T14:30:00+00:00) Getting an array from a string with vanilla JS. Retrieved from https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/

MLA
" » Getting an array from a string with vanilla JS." Go Make Things | Sciencx - Wednesday May 5, 2021, https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/
HARVARD
Go Make Things | Sciencx Wednesday May 5, 2021 » Getting an array from a string with vanilla JS., viewed ,<https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/>
VANCOUVER
Go Make Things | Sciencx - » Getting an array from a string with vanilla JS. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/
CHICAGO
" » Getting an array from a string with vanilla JS." Go Make Things | Sciencx - Accessed . https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/
IEEE
" » Getting an array from a string with vanilla JS." Go Make Things | Sciencx [Online]. Available: https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/. [Accessed: ]
rf:citation
» Getting an array from a string with vanilla JS | Go Make Things | Sciencx | https://www.scien.cx/2021/05/05/getting-an-array-from-a-string-with-vanilla-js/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.