Learn how to convert a list into an array in JavaScript

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa πΏπ¦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

π¨ Real-world use-case: I wanted to convert an excel list into an array; it had 200+ records, so a pain to do manually.
So why not create something easy in JavaScript!
The end goal is to have a textarea where we can input our list and auto-convert it into an array.
This is going to be the result in Codepen:
The HTML is going to be super easy for this, we just need a textarea and a paragraph to output our array.
<div class="container">
<textarea id="textarea">
Paste
your
list
here</textarea
>
<p id="array">["Paste","your ","list ","here"]</p>
</div>
Let's also add some super easy CSS to make it look good:
.container {
display: flex;
align-items: center;
justify-content: center;
height: 100vh;
flex-direction: column;
background: #3a86ff;
}
textarea {
padding: 10px;
min-width: 200px;
width: 50vw;
height: 100px;
margin-bottom: 10px;
border-radius: 10px;
}
#array {
background: #efefef;
border: 1px dashed #333;
padding: 10px;
width: 50vw;
min-width: 200px;
word-wrap: break-word;
margin-bottom: 10px;
color: #ff006e;
border-radius: 10px;
}
Ok, on to the magic part, let's convert our array input into an array.
We start by adding defining our variables
const textarea = document.getElementById('textarea');
const array = document.getElementById('array');
Now we can add our input listener to the textarea element
textarea.addEventListener('input', function() {
var arrayValues = textarea.value.split(/\n/g);
array.innerHTML = JSON.stringify(arrayValues);
});
And then for some magic we will add a click event to our paragraph which will auto select all text.
array.addEventListener('click', function() {
var range = document.createRange();
var selection = window.getSelection();
range.selectNodeContents(array);
selection.removeAllRanges();
selection.addRange(range);
});
An awesome, simple tool, but effective!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter