admin管理员组文章数量:1291592
I currently have an array of strings that are numbers
data.dataArr = [0: " 1,431,417 "
1: " 1,838,127 "
2: " 679,974 "
3: " 2,720,560 "
4: " 544,368 "
5: " 1,540,370 "]
I am attempting to remove the mas so my data returns 0: "1431417" , 1: 1838127 ...
After removing mas I am then mapping this array to convert it to an array of numbers and not strings. But when console.logging the finalArray that should return an array of numbers I am getting NaN
I believe this is due to the part of removing the mas.
Here is my code:
let data = {
dataArr: [
" 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 "
]
};
//removing mas
let arrData = data.dataArr.map(e => e.replace(/(,\s*)+/, ','));
let finalArr = arrData.map(Number);
console.log(finalArr)
I currently have an array of strings that are numbers
data.dataArr = [0: " 1,431,417 "
1: " 1,838,127 "
2: " 679,974 "
3: " 2,720,560 "
4: " 544,368 "
5: " 1,540,370 "]
I am attempting to remove the mas so my data returns 0: "1431417" , 1: 1838127 ...
After removing mas I am then mapping this array to convert it to an array of numbers and not strings. But when console.logging the finalArray that should return an array of numbers I am getting NaN
I believe this is due to the part of removing the mas.
Here is my code:
let data = {
dataArr: [
" 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 "
]
};
//removing mas
let arrData = data.dataArr.map(e => e.replace(/(,\s*)+/, ','));
let finalArr = arrData.map(Number);
console.log(finalArr)
alternatively I've tried :
let arrData = data.dataArr.replace(/,/g, "")
Which resulted in "data.dataArr.replace is not a function"
- 2 You're using invalid data. Arrays are not key:value pairs. – Roko C. Buljan Commented May 7, 2020 at 15:02
7 Answers
Reset to default 6You can use number in same map
callback function, g
to remove all occurrence of ,
dataArr = [" 1,431,417 ", " 1,838,127 ", " 679,974 ", " 2,720,560 ", " 544,368 ", " 1,540,370 "]
let arrData = dataArr.map(e => Number(e.replace(/(,\s*)+/g, '').trim()));
console.log(arrData)
['1,234', '7,8,90,'].map(eachNo => {
return eachNo.split(',').join('')
});
Split() will split string where it will find ,(ma) and will return an array of strings.
join() will operate on array of strings.
There also seems to be a space in your array which would be giving NaN, Remove everything other than digits and maybe a dot.
Try this regex:
e.replace(/[^\d.]/g, '');
Use Array.prototype.map()
const data = {};
data.dataArr = [
" 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 ",
];
const numbersArray = data.dataArr.map(str => Number(str.replace(/\D/g, '')));
console.log(numbersArray)
Maybe like this:
var data = {};
data.dataArr =
[
" 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 "
];
function array_to_numbers(_arr){
var out = [];
for(var key in _arr){
var el = _arr[key];
el = el.trim();
el = el.replace(new RegExp(',','g'), '');
el = parseInt(el)||0;
out.push(el);
}
return out;
}
console.log(array_to_numbers(data.dataArr));
You can .map()
over the array and split up by ,
and then join them at ""
. You also need to wrap everything in Number
to convert it to an number
let dataArr = [
" 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 "
]
let result = dataArr.map(el => Number(el.split(",").join("")))
console.log(result)
let data = new Object();
data.dataArr = [ " 1,431,417 ",
" 1,838,127 ",
" 679,974 ",
" 2,720,560 ",
" 544,368 ",
" 1,540,370 "];
const finalArr = data.dataArr.map(x => x.replace(/[^0-9]/g, ''));
console.log(finalArr)
本文标签: JavascriptRemoving commas from an array of string numbersStack Overflow
版权声明:本文标题:Javascript - Removing commas from an array of string numbers - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741535846a2384021.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论