Home > Web Front-end > JS Tutorial > How Do I Access JSON Properties Containing Dashes in JavaScript?

How Do I Access JSON Properties Containing Dashes in JavaScript?

DDD
Release: 2024-11-23 05:35:16
Original
656 people have browsed it

How Do I Access JSON Properties Containing Dashes in JavaScript?

Accessing JSON Properties with Dashes

Accessing JSON properties with the dash character can be tricky. When trying to access the "profile-id" property as "jsonObj.profile-id," it may result in a "ReferenceError" because the JavaScript interpreter interprets it as a subtraction expression.

The solution is to enclose the property name in square brackets when accessing it, like this:

jsonObj["profile-id"]

This method allows you to access properties with characters that are not valid JavaScript identifiers, such as dashes. Using this format, you will be able to retrieve the value of "profile-id" from the JSON object successfully.

The above is the detailed content of How Do I Access JSON Properties Containing Dashes in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template