[英]string.endswith(“”) does not work in IE(not sure how to use a Polyfill)
我使用string.endswith()
循環遍歷JSON對象,並找出對象的任何屬性endswith
以"Value"
子字符串endswith
。
在找出對象屬性是否以"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;
}, {})
});
上面的代碼是否正確? 如果不是我怎么能改變它來實現我的目標?
您擁有的endsWith
函數實際上是將endsWith
函數附加到本機String
對象,JavaScript允許您這樣做。 它可以讓你endsWith
調用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
函數之前,您只需要包含一些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行檢查您是否需要填充。 在第2行,該函數被賦值給String.prototype.endsWith
,這意味着可以使用String.endsWith(searchString, position)
調用它。
根據要求,您可以將String.prototype.slice()
與參數-5
if (n.slice(-5) === "Value")
或RegExp.prototype.test()
與RegExp
/Value$/
if (/Value$/.test(n))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.