Beginner Query: What is the method for retrieving this data in JavaScript?

I'm struggling with accessing a specific value in an Object in JavaScript for the first time.

The JSON I'm working with is structured like this:

{
   "payload":{
      "params":{
         "switch:0":{
            "output":false,  **<= I need to retrieve this value ("false")**
         }
      }
   },
}

When I look at the object in Node-Red's debug pane, it appears like this:

https://i.sstatic.net/x7pVW.png

I initially thought it was an array and tried accessing it like this:

    value = msg.payload.params.switch[0].output

Unfortunately, I received an error message:

"TypeError: Cannot read property '0' of undefined"

I also attempted:

  value = msg.payload.params.switch

but the value returned was "undefined".

Can someone guide me on the correct way to access the value of "output" in JavaScript? I've tried searching online for answers, but haven't had any luck.

Any assistance would be greatly appreciated!

Answer №1

To retrieve the value of the "switch:0" property, utilize bracket notation as it is not a valid identifier.

const obj = {
   "payload":{
      "params":{
         "switch:0":{
            "output":false, 
         }
      }
   },
}
const result = obj.payload.params['switch:0'].output;
console.log(result);

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

AndroidStudio is obstinate when it comes to recognizing the hierarchy of inheritance

In my project, I am utilizing the library implementation 'org.json:json:20180813' I have developed a class called User which can be converted into a JSON string for data persistence purposes: data class User(override val name:String, override ...

Combining arrays of objects based on a common key

Issue at Hand: Having multiple arrays of objects with the same key and wanting to merge them using jq. Here is an example to illustrate the problem: Original Data [ { "google.com": { "http": { "dest_url": "http://stackoverflow.com" ...

Bringing in an external .js file into nuxt.config.js

Having trouble importing config.js with the project's API keys and getting undefined as a return value. //config.js var config = { fbAPI: "key" } - //nuxt.config.js const cfg = require('./config') env: { fbAPI: cfg.apiKey } Wonder ...

What is the process for advancing to the next or previous step using Angular.js?

I am currently utilizing ui-route for routing purposes, specifically for navigating through a series of sequential forms. After submitting one form, I would like to automatically move on to the next step without hard coding the step name in the $state.go( ...

Clicking a button to reference a specific section within a list item

I'm currently working on developing a todo application using php, ajax, and mysql. I am facing a challenge where I need to implement a button that deletes an item from both the screen and the database. However, I am unsure about how to specify which i ...

Place the `service` parameter into the `run` function

What are some examples of when to utilize the angular.run method? I have a service that is resource-intensive and takes a significant amount of time to initialize before it can be used in conjunction with my view. angular.module('myApp').service ...

Navigating through Sails.js: A comprehensive guide on executing test cases

Being a beginner in sails, node, and js, I may be missing out on some obvious steps. My environment includes sails 0.10.5 and node 0.10.33. Although the sails.js documentation covers tests in , it does not provide instructions on how to actually execute ...

Embed the Nest API into your HTML5 code

My mobile HTML5 application includes a script in the index.html file. Nest.js (code below) Whenever I run my application, I encounter an error stating that Firebase is not defined. This is odd because the function seems simple: --> var dataRef = new ...

Exploring the Benefits of Utilizing External APIs in Next JS 13 Server-side Operations

Can someone provide more detail to explain data revalidation in Next JS 13? Please refer to this question on Stack Overflow. I am currently utilizing the new directory features for data fetching in Next JS 13. According to the documentation, it is recomme ...

Converting a C++ string array to JSON: A step-by-step guide

Recently, I embarked on the journey of incorporating Microsoft Cognitive Services using C++. In my implementation, there exists a C++ String array named faceIds. string faceIds[] ={ "29e874a8-a08f-491f-84e8-eac263d51fe1", "6f89f38a ...

Adding design to distinct element after clicking a button

Struggling with a JS/Jquery issue on my portfolio website, I admit that I am still just an average programmer. My portfolio website has five buttons, each representing a different project. For each project, there is a corresponding text description and an ...

Switch the index of a JSON object within Fragments

I need to update the JSON object index within my QueryUtils.java file as I switch between different Fragments. Specifically, I want to modify this line: JSONObject currentDay = dayArray.getJSONObject(0); I have a total of 7 fragments and I am trying to ...

JavaScript is unable to make changes to the page once it has finished loading

I have implemented a JavaScript code that allows me to send messages in a chat interface: function sendMessageToChat(conversation, message) { $("#content").html(conversation + message); $(".current-msg").hide(); $(".current-msg").delay(500).fadeIn ...

Innovative way to design a menu using HTML, CSS, and JavaScript that dynamically resizes to a specific width

I'm currently working on a website and I'm trying to create a menu with specific width of 700px. However, I'm unsure whether to tackle this using CSS, JavaScript, or a combination of both. Which approach would be the most straightforward for ...

VueJS computed property not updating correctly

My web application, built with Laravel / Vue JS, includes a google map component and a Vuetify data-table component. The Vue instance also features a computed property that calculates points for the Google Map based on another computed property from the Vu ...

Gridsome's createPages and createManagedPages functions do not generate pages that are viewable by users

Within my gridsome.server.js, the code snippet I have is as follows: api.createManagedPages(async ({ createPage }) => { const { data } = await axios.get('https://members-api.parliament.uk/api/Location/Constituency/Search?skip=0&take ...

unable to configure socket.io to start listening on IPv4 address

While setting up IPV4 in Socket.IO, I encountered the following error: /var/www/js/AndroRAT/node_modules/socket.io/lib/index.js:279 opts.path = opts.path || this.path(); ^ TypeError: Cannot create property 'path' on string '0.0.0 ...

Implementing a FadeOut effect for the clicked link

When clicking on each link, I want the same link to fadeOut() after clicking on the ok button in the myalert() function. If clicked on cancel, it should not fadeOut(). How can I achieve this using the myalert() function? For example: http://jsfiddle.net/M ...

Tips for fixing View Encapsulation problem in Angular8

I have a parent component and a child component. The child component is created as a modal component. I have included the child component selector inside the parent component and set the view encapsulation to none so that it will inherit the parent compone ...

Is it possible to convert a Wikipedia table into JSON using the Wikipedia API?

Can you transform this table into a JSON array within an array? I am looking for an output like: [ ["Northwest Caucasian", "Abkhaz", "аҧсуа бызшәа, аҧсшәа", "ab", "abk", "abk", "abk", "abks"], [Afro-Asiatic", "Afar", "Afaraf", "aa", ...