使用JavaScript从数组中存在的字符串中删除双引号 [英] Remove double quotes from the strings present inside the arrays using javascript
问题描述
我有一个像这样的数组:
array = [ apple, orange, pear]
我想从每个引号的开头和结尾删除双引号。数组中的字符串。
array = [apple,orange,pear]
我试图遍历数组的每个元素并像下面这样替换字符串
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.
推荐答案
唯一的
在您的问题中看到的是数组中包含的字符串文字的引号。
The only "
's I see in your Question are the quotes of the String literals contained in your array.
["apple", ...]
^ ^
您可能不知道
字符串文字是计算机程序源代码中字符串值的表示形式。 (维基百科)
大概读了M关于字符串对象
如果您不小心表示调用 JSON的结果.stringify
。
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"]
您可以通过替换他们
var string = JSON.stringify(array);
string.replace (/"/g,''); //"[apple,orange,pear]"
这篇关于使用JavaScript从数组中存在的字符串中删除双引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!