string.endswith("") does not work in IE(not sure how to use a Polyfill)
The polyfill function you have is actually for attaching the endsWith
function to the native String
object, which JavaScript allows you to do. It will allow you to call endsWith
like normal.
Instead of wrapping it in a function, let it run right away, then just use the normal 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; }, {})});
You implemented the polyfill incorrectly.
You just need to include somewhere before using your endsWith
function.
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 whether you need a polyfill. On line 2, the function is assigned to String.prototype.endsWith
, which means it can be called with String.endsWith(searchString, position)
Given requirement you can use String.prototype.slice()
with parameter -5
if (n.slice(-5) === "Value")
or RegExp.prototype.test()
with RegExp
/Value$/
if (/Value$/.test(n))