-1

Hello I'm making something for my internship. But I kinda walked into a wall. For about 4 hours I have tried to turn my code from this:

["word1", "word2", "word3", "word4"]

Into this:

[word1, word2, word3, word4]

I have tried all sorts of a = a.replace( some code ) but I just cant make it work. If you could help me with this i would really appreciate it.

WienyButt
  • 50
  • 5
  • 1
    `word1`, etc... by itself isn't a valid data structure. Are these varaibles in your code? – Nick Parsons Mar 14 '19 at 12:30
  • 1
    `["word1", "word2", "word3", "word4"]` is an array literal with string literals in it. But the string literals do not contain `"` characters `"word1"` the literal contains the string `word1`. So do you have *literals* or actually `"\"word1\""` or `'"word1"'` (a string literal containing qotation marks)? – VLAZ Mar 14 '19 at 12:30
  • How exactly do you expect to use this array ? In javascript strings have to be placed between either simple or double quotes, else here it would be interpreted as variable names. – Sylvain Mar 14 '19 at 12:32
  • above you have an array of 4 strings `"word1"`, `"word2"`, `"word3"` and `"word4"`, bellow you have an array of 4 values which correspond to the values which variables `word1`, `word2`, `word3` and `word4` have in your scope, if they have any. – Dellirium Mar 14 '19 at 12:32
  • What is the type of `["word1", "word2", "word3", "word4"]`? An array of strings or a just a string with `[]` in it? – adiga Mar 14 '19 at 12:32
  • 2
    Might be duplicate of [remove-double-quotes-from-the-strings-present-inside-the-arrays-using-javascript](https://stackoverflow.com/questions/19325430/remove-double-quotes-from-the-strings-present-inside-the-arrays-using-javascript). – ejaz Mar 14 '19 at 12:42
  • Possible duplicate of [Remove double quotes from the strings present inside the arrays using javascript](https://stackoverflow.com/questions/19325430/remove-double-quotes-from-the-strings-present-inside-the-arrays-using-javascript) – Fukiyel Mar 14 '19 at 13:24

2 Answers2

1

An array is a list of items. a string is marked by the quotation marks. this array contains a list of strings, so a list of items that are marked by content encapsulated by quotation marks. The quotation marks are not literally part of the content of the string.

for instance I could store a string in a variable

var str = "this is a string";

but when i choose to log this to the console

console.log(str);

it would not actually output the quotation marks, and the result would be:

this is a string

EDIT:

if what you need is an actual string that looks like

[word1, word2, word3, word4]

you can take every single string in the array, and join them with eachother. then you put the brackets behind and in front and you would get that as an output.

example:

var arr = ["word1", "word2", "word3", "word4"];
var joinedstring = "[" + arr.join(', ') + "]";
Dennis Lukas
  • 483
  • 2
  • 6
  • 20
0

If you have an array written like this: ["string1", "string2", "string3", "string4"], it doesn't mean that the strings in this array contain quotes, the quotes are just used to indicate that each variables in the array is a string and not something else (variable name / integer / other...).

The snippet below shows that "string1" and "string2" both are surrounded with quotes in the array definition but the quote doesn't appear when printing them to the console.

var array = ["string1", "string2"];
array.forEach(function(string) {
  console.log(string);
});
Sylvain
  • 417
  • 7
  • 16