Communities for your favorite technologies. Explore all Collectives
Stack Overflow for Teams is now called Stack Internal. Bring the best of human thought and AI automation together at your work.
Bring the best of human thought and AI automation together at your work. Learn more
Find centralized, trusted content and collaborate around the technologies you use most.
Stack Internal
Knowledge at work
Bring the best of human thought and AI automation together at your work.
I have file called "temperatures.txt". It includes the text:
22, 21, 23
How do I copy the file's content into a JavaScript array?
var temperatures = [22, 21, 23];
\r\n
If your text looks like this
var txt = "22,21,23";
you can convert it to an array like this
var array = txt.split(',');
or, if theres also a new line do
var array = txt.split(',\n');
Add a comment
Your text
var mytxt = "10,20,30";
Convert into array
var array = mytxt .split(',');
var str = "22,\ 21,\ 23"; // this will remove spaces as well var result = str.split(/\s*,\s*/); console.log(result);
Required, but never shown
By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.
Start asking to get answers
Find the answer to your question by asking.
Explore related questions
See similar questions with these tags.
\r\nand you have JS array.