最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove double quotes from the strings present inside the arrays using javascript - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question asked Oct 11, 2013 at 19:05 user2844540user2844540 611 gold badge2 silver badges6 bronze badges 6
  • 3 How are you viewing the Array when it has the quotes? – Paul S. Commented Oct 11, 2013 at 19:06
  • 1 You're seeing a string literal representation, not the value of the string. – SLaks Commented Oct 11, 2013 at 19:07
  • Not quite understanding ur question ,paul.Can you elaborate a bit ?Its basically a hardcoded array of strings. – user2844540 Commented Oct 11, 2013 at 19:08
  • @user2844540: The string values don't actually have quotes. – SLaks Commented Oct 11, 2013 at 19:08
  • 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 Commented Oct 11, 2013 at 19:15
 |  Show 1 more comment

1 Answer 1

Reset to default 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]"
发布评论

评论列表(0)

  1. 暂无评论