How can I assign a variable to a JSON value with spaces in its name?

I have a variable

let importantData = data[selectedItem].attributes[0];

I need to link it to various information within a JSON dataset in order to retrieve the value

when I execute the following code, everything works smoothly

let electricityPlant = uniqueData.Total;
console.log(electricityPlant)

However, there are some values that contain spaces like

Main source of electricity and heat production: "1.4"

How can I connect it to importantData?

importantData.Main source of electricity and heat production

results in errors being thrown

Answer №1

specificData['Primary operation for generating electricity and heat']

Answer №2

Essentially, in your specific situation, the value doesn't qualify as a valid JavaScript identifier, which means you'll need to utilize square brackets notation:

"In JavaScript, an object property name can consist of any valid string, or something that can be converted to a string, even if it's just an empty string. However, if the property name isn't a valid JavaScript identifier (such as containing a space or a hyphen, or starting with a number), then accessing it will require using square bracket notation. This method is especially useful when dynamically determining property names (e.g. when the property name isn't known until runtime)."

You may find this article helpful: Working with objects

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Tips for combining several JSON objects in a Node.js/Jade merge operation

Given the code below which sets up various configurations for a company: default.js (utilized by config.js to load base configurations) { "templateData": { "corp": { "corpName": "Company", "DepartmentOne": { "name": "Dep ...

Ways to avoid encoding Unicode characters in JavaScript programming

When I retrieve data from an API, I receive a STRING like this: [ { "reason": "Invalid address", "email": "j\u00c3\u00a9r\u00c3\u00b4mel\u00c3\u00a4ufer@com" }, { "reason": "Invalid address", "email": ...

jQuery show/hide function malfunctioning

Currently, I am attempting to create a basic show/hide interaction for a div using jQuery. The goal is to make the artists-home div vanish and be replaced by the .ken-gallery when the .artist-ken is clicked. Here is my current code, although it is not fun ...

Transferring data to Bigquery using Python

I am currently working on a Python script that pulls data from Firebase, processes it, and then saves it into a JSON file. I have successfully uploaded the JSON file to BigQuery via the command line, but now I want to streamline the process by integrating ...

The Ultimate Guide to Initializing Variables in UI Router State Access

In my application, I have defined 2 states. One is "tickets" which matches /tickets ... $stateProvider // defines the states of my application .state("tickets", { // assigns properties to each state url: "/tickets", // route templateUrl: "m ...

Looping animations using AngularJS

I have implemented a custom directive to trigger an animation on an element when a specific field is empty on the page. However, I am facing an issue where the animation only works once when the user clicks the button with the directive. Subsequent clicks ...

Creating a Cancel Button in JSP/HTML/JS/CSS Form: A Step-by-Step Guide

It is necessary to incorporate the functionality of "save" and "cancel" in the JSP code for this particular form. By selecting "save", the data entered into the form will be submitted to its intended destination. Alternatively, choosing "cancel" will dismi ...

The child element is absolutely positioned within the parent element with a higher z-index

It is crucial for the parent absolute to be under child absolute. How can I resolve this issue using CSS? The positions of both elements must remain absolute. <div class="parent"> <div class="child">child</div> </div> ...

Insert fresh div elements chronologically using jQuery

When a user clicks on the ".u-post-button" button, this script is triggered. It retrieves the content of the comment input field and posts it to the server as a JSON object. The returned data containing the comment information is then appended after the ...

Error in React .js: Unable to access property 'name' as it is undefined

I keep encountering this issue: Uncaught TypeError: Cannot read property 'name' of undefined In my code, I have a user object defined in the App.js file. However, when I attempt to access its properties within my Person component, it throws a ...

Prevent Editing of Input Box in JQuery UI Sortable

Having some trouble placing an input box within a sortable UL. The sortable functionality is working, but the text seems to be "locked" or the click event is overridden...unsure of how to proceed. Came across this link which touches on my current issue. ...

Access within the identical window (PHP file) as opposed to an Iframe

I am currently working with a PHP file that retrieves data from my database. <?php $cdb = new PDO('mysql:dbname=xxx;host=localhost', 'xxx', 'xxx'); foreach ($cdb->query("SELECT * FROM images ORDER BY posted DESC LIMIT ...

The error message reads: "Attempting to call a class constructor Model without using the 'new' keyword in Sequelize 5 and Babel 7 results in a TypeError."

Currently working on a new project utilizing nodejs/express/sequelize. I'm using babel 7 to compile my ES6 code, and everything has been running smoothly with sequelize except for an issue that arises when attempting to use the removeHook method. When ...

Troubleshooting AngularJS form submission with missing data

Is there a way to modify AngularJS to save form data into a database using PHP as the backend? Below is the HTML form code: <form ng-submit="newContactSubmit()"> <label>FirstName<input type="text" name="contact_firstname" required n ...

Code to track the changes made to a specific cell in Google Sheets

I am currently collaborating on a file with multiple individuals and I would like to create a script that can identify the person who last modified a specific cell, along with the timestamp of when the modification occurred. All I need is the email address ...

Exploring Javascript's potential with for loops and arrays

Looking to create a webpage with the following features: (generate question)...this will be a button ? x 1 = ( ) ? x 2 = ( ) ... ? x 9 = ( ) (check answer)...this will also be a button The user starts by selecting a number after clicking th ...

Handle exceptions within jQuery code

Take a look at the code snippet below: <script type="text/javascript> $(document).ready(function() { $("#Save").click(function() { $.post("url", { "data": "data" }, function(data) { ...

What is the best way to iterate through one level at a time?

Imagine a scenario where the structure below cannot be changed: <xml> <element name="breakfast" type="sandwich" /> <element name="lunch"> <complexType> <element name="meat" type="string" /> <element name="vegetab ...

Utilizing Office.js: Incorporating Angular CLI to Call a Function in a Generated Function-File

After using angular-cli to create a new project, I integrated ng-office-ui-fabric and its dependencies. I included in index.html, added polyfills to angular.json, and everything seemed to be working smoothly. When testing the add-in in Word, the taskpane ...

A guide on traversing through nested HTML divs using jQuery

I am facing an issue with nested HTML div elements on my website. The data is being fetched from a JSON file and stored in a variable. For example, the 'obj' variable contains an array of objects with properties such as Name, Progress, Descripti ...