javascript - Replace multiple string items in the array -
i have array shows value "135_1,undefined,undefined"
i have find "undefined" in above array , replace "0_0".undefined can occur multiple times in array.
i used
var = myvariable.replace("undefined", "0_0"); alert(extra);
but have use 3 times every single time can search 1 , replace it.
i have used this::
(var = 0; < myvariable.length; i++) { alert(myvariable[i]); myvariable[i] = myvariable[i].replace(/undefined/g, '0_0'); } alert(myvariable);
but did'nt solved purpose.
string.prototype.replace
method accessible strings. undefined
not string.
this might you.
for (var i=0, len=arr.length; i<len; i++) { if (arr[i] === undefined) { arr[i] = "0_0"; } } alert(json.stringify(arr));
you use array.prototype.map this. note, works in ie >= 9
arr = arr.map(function(elem) { return elem === undefined ? "0_0" : elem; });
Comments
Post a Comment