There are a number of problems here:
- Your second string literal is broken. Use
"'"
or '\''
.
- when used with a string as the first parameter
.replace
will only replace the first instance found. To replace all instances, use a regular expression with the g
flag.
- Finally, you're not actually modifying the array in any way because the
.replace
method returns a new string. Try a simple for
-loop instead.
In the end your code should look something like this:
for (var i = 0; i < Arr1.length; i++)
Arr1[i] = Arr1[i].replace(/"/g,"'");
Given your update, the nature of the question has changed significantly. It now seems that what you want is to simply convert numbers in your array to strings. For that, just use the toString
method:
for (var i = 0; i < Arr1.length; i++)
Arr1[i] = Arr1[i].toString();
Or for brevity, concatenate the value with an empty string:
for (var i = 0; i < Arr1.length; i++)
Arr1[i] = Arr1[i] + "";
But note, this will remove trailing 0
's. A number like 1.0
will be converted to the string like "1"
. To ensure that the trailing decimals are not trimmed, use toPrecision
:
for (var i = 0; i < Arr1.length; i++)
Arr1[i] = Arr1[i].toPrecision(2);