Remove double quotes from the strings present inside the arrays using javascript

user2844540 picture user2844540 · Oct 11, 2013 · Viewed 33.7k times · Source

I have an array like this: array = ["apple","orange","pear"] I want to remove the double quotes from the beginning and end of each one of the strings in the array. array = [apple,orange,pear] I tried to loop through each element of the array and did a string replace like the following

    for (var i = 0; i < array.length; i++) {
        array[i] = array[i].replace(/"/g, "");
    }

But it did not remove the double quotes from the beginning and end of the string. Any help would be appreciated.Thanks much.

Answer

Moritz Roessler picture Moritz Roessler · Oct 11, 2013

The only "'s I see in your Question are the quotes of the String literals contained in your array.

["apple", ...]
 ^     ^

You probably aren't aware that

A string literal is the representation of a string value within the source code of a computer program.(Wikipedia)

and should probably read the MDN article about the String object


If you by accident mean the result of calling JSON.stringify on your array.

var array = ["apple","orange","pear"];
JSON.stringify (array); //["apple", "orange", "pear"]

You can do so by replacing them

var string = JSON.stringify(array);
    string.replace (/"/g,''); //"[apple,orange,pear]"