Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 802
user2844540 Avatar asked Oct 11 '13 19:10

user2844540


1 Answers

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]"
like image 100
Moritz Roessler Avatar answered Oct 20 '22 16:10

Moritz Roessler