string.endswith("") 在 IE 中不起作用(不确定如何使用 Polyfill)
string.endswith("") does not work in IE(not sure how to use a Polyfill)
我正在使用 string.endswith()
遍历 JSON 个对象并找出对象 endswith
的任何 属性 是否是一个 "Value"
子字符串。
在确定对象 属性 是否以 "Value"
结尾后,我试图将 属性 的值四舍五入为 2 位小数,默认情况下为 5 位小数。
这是我的代码
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;
}, {})
});
上面的代码在 chrome
和 Firefox
中工作得很好,而 IE 它抛出 endsWith
不是函数异常。
我发现使用了 pollyfills 来解决这些问题,我尝试过但失败了。我什至不确定我是否正确使用了 pollyfill。
这就是我的做法,
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;
}, {})
});
上面的代码是否正确?如果不是,我该如何更改它以实现我的 objective?
您拥有的 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;
}, {})
});
您错误地实现了 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;
};
}
第 1 行检查您是否需要 polyfill。在第 2 行,该函数被分配给 String.prototype.endsWith
,这意味着它可以用 String.endsWith(searchString, position)
来调用
根据要求,您可以使用带有参数 -5
的 String.prototype.slice()
if (n.slice(-5) === "Value")
或RegExp.prototype.test()
与RegExp
/Value$/
if (/Value$/.test(n))
我正在使用 string.endswith()
遍历 JSON 个对象并找出对象 endswith
的任何 属性 是否是一个 "Value"
子字符串。
在确定对象 属性 是否以 "Value"
结尾后,我试图将 属性 的值四舍五入为 2 位小数,默认情况下为 5 位小数。
这是我的代码
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;
}, {})
});
上面的代码在 chrome
和 Firefox
中工作得很好,而 IE 它抛出 endsWith
不是函数异常。
我发现使用了 pollyfills 来解决这些问题,我尝试过但失败了。我什至不确定我是否正确使用了 pollyfill。
这就是我的做法,
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;
}, {})
});
上面的代码是否正确?如果不是,我该如何更改它以实现我的 objective?
您拥有的 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;
}, {})
});
您错误地实现了 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;
};
}
第 1 行检查您是否需要 polyfill。在第 2 行,该函数被分配给 String.prototype.endsWith
,这意味着它可以用 String.endsWith(searchString, position)
根据要求,您可以使用带有参数 -5
String.prototype.slice()
if (n.slice(-5) === "Value")
或RegExp.prototype.test()
与RegExp
/Value$/
if (/Value$/.test(n))