String.endswith ("") doesn't work in IE (not sure how to use Polyfill)
I am using string.endswith()
to loop through JSON objects and find out if there is any substring property of endswith
a object "Value"
.
After figuring out if an object property ends with "Value"
, I am trying to round the property value to 2 decimal places, which is 5 decimal places by default.
Here is my code
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;
}, {})
});
The above code works absolutely fine in chrome
and Firefox
although IE it throws endswith
is not a feature exception.
I found out that pollyfills are used for problems like this, which I tried and I failed. I'm not even sure if I was using pollyfill correctly.
so this is how i did it,
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;
}, {})
});
is the above code even correct? if not, how can I change it to achieve my goal?
source to share
The polyfill function you have is actually for attaching a function endsWith
to its own object String
that allows you to execute JavaScript. This will allow you to call endsWith
as usual.
Instead of wrapping it in a function, let it run right away and then just use the regular one 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;
}, {})
});
source to share
You have applied the polyfill incorrectly.
You just need to point somewhere before using the function 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;
};
}
Line 1 checks if you need a polyfill. On line 2, the function is assigned String.prototype.endsWith
, which means it can be called withString.endsWith(searchString, position)
source to share