string 使用javascript从数组中存在的字符串中删除双引号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19325430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 02:07:12  来源:igfitidea点击:

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

javascriptarraysstringdouble-quotes

提问by user2844540

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

我有一个这样的数组: array = ["apple","orange","pear"] 我想从数组中每个字符串的开头和结尾删除双引号。array = [apple,orange,pear] 我尝试遍历数组的每个元素并进行如下字符串替换

    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.

但它并没有从字符串的开头和结尾删除双引号。任何帮助将不胜感激。非常感谢。

回答by Moritz Roessler

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

并且应该阅读有关String 对象的 MDN 文章



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

如果您不小心表示调用JSON.stringify数组的结果。

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