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

喜欢而已 提交于 2020-01-02 04:31:14

问题


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.


回答1:


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]"


来源:https://stackoverflow.com/questions/19325430/remove-double-quotes-from-the-strings-present-inside-the-arrays-using-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!