如何修复字符串.replace不是nodejs中的函数



我有一些数据,想用我目前正在使用下面的代码的空格替换$

variable - theirValue = data.lowest_price.replace("$",'').trim();

它的.replacement有问题,并表示data.replacement不是的函数

这是所在位置的完整代码

const market = require('steam-market-pricing');
theirValue= 0;
market.getItemsPrice(730, 'MP9 | Storm (Minimal Wear)', function(data) {
console.log(data);
theirValue += data.lowest_price.replace("$",'').trim()
})

上面的代码返回的是下面的json形式的

{ 'MP9 | Storm (Minimal Wear)':
{ success: true,
lowest_price: '$0.05',
volume: '185',
median_price: '$0.03' } }

这就是它在文件中的方式,我在代码中的方式。我正在做的是获取我放置的物品的蒸汽市场价格,以代替p90名称,但我希望它只显示价格(例如1.00),而不是$price(例如1.00美元)控制台上说更换不是功能

问题是:

data['MP9 | Storm (Minimal Wear)'].lowest_price // '$0.05'
data.lowest_price                               // undefined

market.getItemsPrice()是否应该使用第二个参数来返回包含lowest_price的对象?


您的评论提出了关于JavaScript中类型转换的第二个问题。请记住,lowest_price存储为字符串。当添加两种不同的类型(数字和字符串)时,如果可以的话,它会尝试强制转换值:

theirValue += '0.05'                 // number + string --> string: '00.05'
theirValue += parseFloat('0.05', 10) // number + number --> number: 0.05
theirValue += +'0.05'                // number + number --> number (unary operator)

最新更新