我正在使用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
不是函数异常。
我发现了这些问题,使用了pollyfill,我试过,但我失败了。我甚至不确定我是否正确使用了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;
}, {})
});
上面的代码是否正确?如果不是我怎么能改变它来实现我的目标?
答案 0 :(得分: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 :(得分:1)
您错误地实施了填充。
在使用Town
函数之前,您只需要包含一些内容。
endsWith
第1行检查您是否需要填充。在第2行,该函数已分配给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;
};
}
,这意味着可以使用String.prototype.endsWith
答案 2 :(得分:0)
根据要求,您可以将String.prototype.slice()
与参数-5
if (n.slice(-5) === "Value")
或RegExp.prototype.test()
与RegExp
/Value$/
if (/Value$/.test(n))