5

How to remove double quotes from array in JavaScript?

Suppose this is an array

enc= ["WPA2", "WPA2", "WPA2", "WPA2", "WPA1", "WEP", "WPA2", "WPA2", "WPA1", "WEP", "WEP"]

Thanks

Any help would be appreciated.

vcsjones
  • 138,677
  • 31
  • 291
  • 286
Mohd Anas
  • 634
  • 1
  • 9
  • 22

2 Answers2

25

There are no double quotes in that array. The quotes just delimit the string literals, when they are parsed into strings they don't have quotes in them.


If you wanted to remove all the quotes from a string which actually had some in it:

str = str.replace(/"/g, "");  // RegEx to match `"` characters, with `g` for globally (instead of once)

You could do that in a loop over an array:

for (var i = 0; i < enc.length; i++) {
    enc[i] = enc[i].replace(/"/g, "");
}

If you wanted to change the source code so that it looked like:

enc= [WPA2, WPA2, WPA2, WPA2, WPA1, WEP, WPA2, WPA2, WPA1, WEP, WEP]

… (and populated the array with some predefined variables) then you would be too late. The source code would have already been parsed by the JavaScript engine.

To get at variables when you have their names in strings, you would have to enter the murkey world of variable variables and are better off refactoring to use object properties (or going direct to an array).

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
0

This would be an error. Quotes are needed to mark the content beeing a string, not a variable.

Thomas Junk
  • 5,588
  • 2
  • 30
  • 43
  • 7
    It wouldn't be a syntax error; they are all valid identifier names. It might be a *reference* error, but only if they were undeclared – Quentin May 31 '13 at 13:19
  • @Thomas Junk I know but i want to store arraly like {De:3,EN::5 }; then how is possible ..Please help –  Nov 01 '17 at 06:58