1

I have a JS object and I want to add to it a field named "Content-Type". I add it using the bracket notation as usual.

params["Content-Type"] = "application/json"

But the problem that I am facing is that, this field gets added as a string. Like this

Id: 'de311',
Key: 'Idn71bf9',
'Content-Type': 'image/jpeg'

The Content-Type field is being added as a string (with single quotes), which is not accessible I guess.

If the question isn't clear I am happy to explain.

Om Gupta
  • 192
  • 2
  • 8
  • 4
    In your case property `"Content-Type"` of `params` can be accessed via `params["Content-Type"]`. It cannot be accessed by dot operator like `params.Id` since `Content-Type` is not a valid variable name though. – SdtElectronics Jan 04 '21 at 08:20
  • Does this answer your question? [JavaScript property access: dot notation vs. brackets?](https://stackoverflow.com/questions/4968406/javascript-property-access-dot-notation-vs-brackets) – Krzysztof Krzeszewski Jan 04 '21 at 08:24
  • 2
    I assume your getting this in the debugger? Don't worry it's just how it will display it, there is no extra double quotes, it's just how it's been serialised for display. – Keith Jan 04 '21 at 08:28

1 Answers1

1

That's perfectly normal and usable - it's just shown as a string in the debugger/console you're using because Content-Type isn't a valid variable name, and therefore cannot be displayed as such. You can only access a property called Content-Type through bracket notation - other properties, such as Id, you could access through either params.Id or params["Id"] - but due to the nature of the name of the property, and how it can't be used as a valid variable name in JavaScript, it's displayed with quotes.

tl;dr just a visual issue, it's performing exactly how you want it to be.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79