如何使用美元符号和逗号的替换方法?
How to use replace method for dollar sign and comma?
我想完成两件事,将余额解析为整数(我假设这是必需的),然后使用 reduce 方法将总余额相加。我不知道删除逗号的最佳方法?或者我应该改用 splice 吗?
如果 reduce 方法现在添加它们,它只会在逗号前添加第一个数字,因此 1,1,8.
const data = [{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const balances = data.map((amount) => {
var newAmount = parseFloat(amount.balance.replace(/$/g, ""));
return newAmount;
});
console.log(balances);
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(balances.reduce(reducer));
这将尝试删除 $ 并将余额相加并显示出来。但是我也不知道如何删除逗号(应该修复它)?
使用替换
const data = [
{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => parseFloat(o.balance.replace(/[$,]/g, "")))
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
使用匹配
const data = [{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => {
return +o.balance.match(/[\d]+/g).join("");
})
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
或
const data = [
{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => parseFloat(o.balance.match(/[\d]+/g).join("")))
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
您可以使用 character class [$,]
接受方括号内的任何一个字符。
const getAmount = (amount) => {
return parseFloat(amount.replace(/[$,]/g, ""));
}
console.log(getAmount("3,45.132"));
我想完成两件事,将余额解析为整数(我假设这是必需的),然后使用 reduce 方法将总余额相加。我不知道删除逗号的最佳方法?或者我应该改用 splice 吗? 如果 reduce 方法现在添加它们,它只会在逗号前添加第一个数字,因此 1,1,8.
const data = [{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const balances = data.map((amount) => {
var newAmount = parseFloat(amount.balance.replace(/$/g, ""));
return newAmount;
});
console.log(balances);
const reducer = (accumulator, currentValue) => accumulator + currentValue;
console.log(balances.reduce(reducer));
这将尝试删除 $ 并将余额相加并显示出来。但是我也不知道如何删除逗号(应该修复它)?
使用替换
const data = [
{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => parseFloat(o.balance.replace(/[$,]/g, "")))
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
使用匹配
const data = [{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => {
return +o.balance.match(/[\d]+/g).join("");
})
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
或
const data = [
{
index: "14",
name: "Bob",
balance: ",000",
},
{
index: "23",
name: "John",
balance: ",200",
},
{
index: "17",
name: "Steve",
balance: ",000",
},
];
const result = data
.map((o) => parseFloat(o.balance.match(/[\d]+/g).join("")))
.reduce((acc, curr) => acc + curr, 0);
console.log(result);
您可以使用 character class [$,]
接受方括号内的任何一个字符。
const getAmount = (amount) => {
return parseFloat(amount.replace(/[$,]/g, ""));
}
console.log(getAmount("3,45.132"));