6

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.

user2844540
  • 61
  • 1
  • 2
  • 6
  • 3
    How are you viewing the _Array_ when it has the quotes? – Paul S. Oct 11 '13 at 19:06
  • 1
    You're seeing a string literal representation, not the value of the string. – SLaks Oct 11 '13 at 19:07
  • Not quite understanding ur question ,paul.Can you elaborate a bit ?Its basically a hardcoded array of strings. – user2844540 Oct 11 '13 at 19:08
  • @user2844540: The string values don't actually have quotes. – SLaks Oct 11 '13 at 19:08
  • When are you seeing the quotes? What are you using to output? – Colin DeClue Oct 11 '13 at 19:13
  • 1
    Notice what Slaks tells you, if you remove the quotes then you will have an array of variables, and if those variables are not defined in your code, you will have an array of errors. Let´s go back to the beginning, why do you need to remove those quotes? Your answer will allows us to help you. – Guillermo Oct 11 '13 at 19:15

1 Answers1

12

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]"
Moritz Roessler
  • 8,542
  • 26
  • 51