Javascript 为什么我会收到一条错误消息,指出 .replace 不是函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3141337/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:24:32  来源:igfitidea点击:

Why do I get an error message that .replace is not a function?

javascriptjqueryfunctionreplace

提问by anleon

I have this function:

我有这个功能:

function countLitreKgSums(cProductIds){
  var cLitreKgSums = new Array();
  var cWeek = 0;
  for(i=0;i<cProductIds.length;i++){
    var cLitreKgSum = 0;
    $("#plan_table td[class='week']").each(function(){
            cWeek = $(this).html();
            var cLitreKgValue = $("input[name*='plan_table_week" + cWeek + "_prod" + cProductIds[i] + "_']").val();
            if (cLitreKgValue == "") {
                cLitreKgValue = 0;
            }
            cLitreKgValue = cLitreKgValue.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/\s/g, '');
            cLitreKgValue = parseFloat(cLitreKgValue);
            cLitreKgSum += cLitreKgValue;
       });
       cLitreKgSum = Math.round(cLitreKgSum * 100) / 100;
       cLitreKgSums[i] = cLitreKgSum;
  }
  return cLitreKgSums;
  //console.log(cLitreKgSums);
}

I get error message that .replace is not a function but in other functions it works as it should. What is the difference?

我收到错误消息,指出 .replace 不是一个函数,但在其他函数中它可以正常工作。有什么不同?

回答by Andrzej Doyle

cLitreKgValuemight be a number at the point where you try to call replaceon it, not a string. In which case, the error is correct - numbers don't have a replacemethod.

cLitreKgValue可能是您尝试调用replace它时的数字,而不是字符串。在这种情况下,错误是正确的 - 数字没有replace方法。

回答by Jonathon Faust

Change this:

改变这个:

cLitreKgValue.replace(/,/g, '.')

to

("" + cLitreKgValue).replace(/,/g, '.')

回答by Joao B

you can use the function String()
i.e.; String(cLitreKgValue).replace ....
String function:
http://www.w3schools.com/jsref/jsref_string.asp

您可以使用函数 String()
即;String(cLitreKgValue).replace ....
字符串函数:http:
//www.w3schools.com/jsref/jsref_string.asp

回答by Nick Craver

While the other answers work (and are correct, numbers don't have .replace(), it's a Stringmethod), I think an overall structure change is better, like this:

虽然其他答案有效(并且是正确的,数字没有.replace(),这是一种String方法),但我认为整体结构变化更好,如下所示:

$("#plan_table td[class='week']").each(function(){
    cWeek = $(this).html();
    var cLitreKgValue = $("input[name*='plan_table_week" + cWeek + "_prod" + cProductIds[i] + "_']").val();
    if (cLitreKgValue !== "") {
      cLitreKgValue = cLitreKgValue.replace(/,/g, '.').replace(/[^\d\.]/g, '').replace(/\s/g, '');
      cLitreKgSum += parseFloat(cLitreKgValue);
    }
});

There's no reason to do all that work when you know it's 0and doesn't affect the result, so if ""means 0and anything += 0has no net effect, just skip it :)

当你知道它0不会影响结果时,没有理由做所有这些工作,所以如果""意味着0并且anything += 0没有实际影响,只需跳过它:)