-2

I have a scenario. I will get all the data from service in the below shown array format. but I need to convert that to by removing them as strings

var array1 = ["one","two","three","four"]

var array1 =  [one,two,thee,four] 

array contains strings to variables of the array.

Shaik Md N Rasool
  • 484
  • 1
  • 5
  • 13

2 Answers2

2

Try,

var array1 = ["one","two","three","four"].map(function(val){ return window[val]; });

But the new array would contain the values of the variables not the variable itself.

Rajaprabhu Aravindasamy
  • 66,513
  • 17
  • 101
  • 130
0

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 < array1.length; i++) {
    array1[i] = array1[i].replace(/"/g, "");
}
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55