'How to remove double quotes from beginning and end of numbered value in a list [duplicate]
I have data as follow:
let str = "1,2,3,4"
and, I want to transform it into
arr = [1,2,3,4]
Solution 1:[1]
You can use split() and Number()
let str = "1,2,3,4";
const output = str.split(',').map(Number);
console.log(output);
Solution 2:[2]
let str = "1,2,3,4"
const nums = str.split(',').map( num => parseInt(num) );
console.log(nums)
You have to split by comma , then you can map through the array of strings and parseInt() them one by one
Solution 3:[3]
actually what you can use is split + map in order to create an array with the values, because they would be strings, you can use + operator to cast it to a number.
like this:
let str = "1,2,3,4"
let newArray = str.split(',').map(n=> +n);
console.log(newArray);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Zohaib Ijaz |
| Solution 2 | Kevin.a |
| Solution 3 | Prince Hernandez |
