使用javascript从数组内部的字符串中删除双引号

时间:2021-11-16 06:18:23

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.

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

1 个解决方案

#1


9  

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)

字符串文字是计算机程序源代码中字符串值的表示。(Wikipedia)

and should probably read the MDN article about the String object

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


If you by accident mean the result of calling JSON.stringify on 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]"

#1


9  

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)

字符串文字是计算机程序源代码中字符串值的表示。(Wikipedia)

and should probably read the MDN article about the String object

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


If you by accident mean the result of calling JSON.stringify on 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]"