string.endswith("")在IE浏览器中无法使用(不确定如何使用Polyfill)

3
我正在使用string.endswith()循环遍历JSON对象,并查找对象的任何属性是否以"Value"子字符串结尾。 在找出对象属性是否以"Value"结尾后,我试图将属性的值四舍五入到两个小数位,默认情况下为五个小数位。 以下是我的代码:
var MyObj= [{"$id":"1","GeoName":"EAST","ReachValue":87.88221970554928,"ReachValue":90.71955219607294,"DepthValue":18.44377295716579,"ShareValue":16.732108234801206},{"$id":"2","GeoName":"WEST","ReachValue":87.88221970554928,"ReachValue":90.71955219607294,"DepthValue":18.44377295716579,"ShareValue":16.732108234801206}];
Obj = JSON.parse(JSON.stringify(MyObj)).map(function (e) {
            return Object.keys(e).reduce(function (p, n) {
                if (n.endsWith("Value"))
                    p[n] = Math.round(e[n] * 100) / 100;
                else
                    p[n] = e[n];
                return p;
            }, {})
        });

以上代码在 chromeFirefox 中完全正常工作,但在 IE 中会抛出 endsWith 不是函数的异常。

我发现对于这种问题,可以使用 polyfill,但我尝试过并且失败了。我甚至不确定我是否正确地使用了 polyfill。

这是我如何做到的:

function polyFillEndswith(searchString, position) {
            if (!String.prototype.endsWith) {
                String.prototype.endsWith = function (searchString, position) {
                    var subjectString = this.toString();
                    if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
                        position = subjectString.length;
                    }
                    position -= searchString.length;
                    var lastIndex = subjectString.lastIndexOf(searchString, position);
                    return lastIndex !== -1 && lastIndex === position;
                }
            }
        }

Obj = JSON.parse(JSON.stringify(BubbleObj)).map(function (e) {
                return Object.keys(e).reduce(function (p, n) {
                    if (n.polyFillEndswith("Value", "0"))
                        p[n] = Math.round(e[n] * 100) / 100;
                    else
                        p[n] = e[n];
                    return p;
                }, {})
            });
上述代码是否正确?如果不正确,我该如何更改以实现我的目标?

他们提到他们尝试过,但没有成功。我猜他们是指他们尝试了他们分享的代码。@MichałPerłakowski - samanime
3个回答

阿里云服务器只需要99元/年,新老用户同享,点击查看详情
4
你所拥有的 polyfill 函数实际上是为了将 endsWith 函数附加到本地 String 对象上,这是 JavaScript 允许你执行的操作。这将使你可以像正常情况下一样调用 endsWith。 不要将其包装在一个函数中,而是让它立即运行,然后只需使用普通的 endsWith:
if (!String.prototype.endsWith) {
    String.prototype.endsWith = function (searchString, position) {
        var subjectString = this.toString();
        if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
            position = subjectString.length;
        }
        position -= searchString.length;
        var lastIndex = subjectString.lastIndexOf(searchString, position);
        return lastIndex !== -1 && lastIndex === position;
    }
}

Obj = JSON.parse(JSON.stringify(BubbleObj)).map(function (e) {
    return Object.keys(e).reduce(function (p, n) {
        if (n.endsWith("Value"))
            p[n] = Math.round(e[n] * 100) / 100;
        else
            p[n] = e[n];
        return p;
    }, {})
});

1
你实现的polyfill有误。 在使用endsWith函数之前,只需要在某个地方包含即可。
if (!String.prototype.endsWith) {
  String.prototype.endsWith = function(searchString, position) {
      var subjectString = this.toString();
      if (typeof position !== 'number' || !isFinite(position) || Math.floor(position) !== position || position > subjectString.length) {
        position = subjectString.length;
      }
      position -= searchString.length;
      var lastIndex = subjectString.lastIndexOf(searchString, position);
      return lastIndex !== -1 && lastIndex === position;
  };
}

第一行检查您是否需要使用polyfill。在第二行,该函数被分配给String.prototype.endsWith,这意味着它可以通过String.endsWith(searchString, position)进行调用。


1
给定要求,您可以使用带有参数-5String.prototype.slice()
if (n.slice(-5) === "Value")
使用 RegExp.prototype.test() 和正则表达式 /Value$/
if (/Value$/.test(n))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,