Replace string in javascript array
You can simply do:
array = ["erf,","erfeer,rf","erfer"];
array = array.map(function(x){ return x.replace(/,/g,"") });
Now Array Becomes:
["erf", "erfeerrf", "erfer"]
Yes.
for(var i=0; i < arr.length; i++) {
arr[i] = arr[i].replace(/,/g, '');
}
The best way nowadays is to use the map()
function in this way:
var resultArr = arr.map(function(x){return x.replace(/,/g, '');});
this is ECMA-262 standard. If you nee it for earlier version you can add this piece of code in your project:
if (!Array.prototype.map)
{
Array.prototype.map = function(fun /*, thisp*/)
{
var len = this.length;
if (typeof fun != "function")
throw new TypeError();
var res = new Array(len);
var thisp = arguments[1];
for (var i = 0; i < len; i++)
{
if (i in this)
res[i] = fun.call(thisp, this[i], i, this);
}
return res;
};
}