Using a variable to retrieve results from an array leads to an undefined outcome

I am encountering an issue with accessing arrays returned from a `GET` response. The names of these arrays may vary depending on the request, so I have a variable that specifies which array to use.

Unfortunately, I keep receiving 'undefined'. For example:

            console.log(current); // trips_out_201702
            console.log(theResponse.current); // undefined
            console.log(theResponse.trips_out_201702); // this works

How can I modify `theResponse.current` to properly return what `current` represents? Why is it returning 'undefined' in this case?

Answer №1

Utilizing square bracket notation can be helpful when accessing a property in an object with a variable as the key.

  console.log(theResponse[current]);

Answer №2

When accessing with a dynamic attribute, it is important to use the syntax:

theResponse[current] instead of theResponse.current

Answer №3

Attempting to access an array value using object notation. Give this a shot instead: variable['keyName'] Best of luck!

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

After updating next.config, the NextJS + NextAuth middleware doesn't seem to be triggered

I'm currently utilizing <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="b0ded5c8c49dd1c5c4d8f0849e81889e87">[email protected]</a> & <a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemai ...

Issue encountered when attempting to pass more than one route parameter in a URL path

My goal is to retrieve data based on the values of 3 fields ("nkk", "nik", and "nama"). In the voters-ctrl: getVotersByParams = async (req, res) => { const nkk = req.params.nkk const nik = req.params.nik const nama = req.params. ...

Creating an extra dialogue box when a button is clicked in React

Having an issue with displaying a loading screen pop-up. I have an imported component named LoadingDialog that should render when the state property "loading" is true. When a user clicks a button on the current component, it triggers an API call that chang ...

Switch between using a dropdownlist and textbox depending on the selection in another dropdownlist

In the process of developing an application, I have implemented a country dropdown list and a state dropdown list. Here's the scenario: when a user selects a country, the states for that country will populate in the state dropdown list. However, the ...

Preventing Repeated Clicks in AngularJS

Looking for a better approach to handle double clicks in AngularJS other than using ng-disabled. I have a solution to disable double/multi-click functionality on button click and re-enable it after completing an ajax process. Any suggestions? When our cod ...

Retrieving vector layers by class and attempting to refresh them in OpenLayers version 2.14 is unsuccessful

First, the process involves accessing all Vector layers, checking if they are visible and retrieving their names. After that, we need to clear any applied filters on those layers and refresh them. Below is a snippet of the code: var mLayers = map.getLaye ...

In React Native, I must include individual radio buttons for each item retrieved from the API

When I select a radio button on mobile, all radio buttons get selected instead of just the one clicked. Here is the current behavior: https://i.stack.imgur.com/qRJqV.jpg The expected behavior is to have only the clicked radio button selected, like in thi ...

Issue with Google Maps API - Error: google object is undefined

Currently, I am in the process of learning how to develop Google Maps by analyzing some basic examples provided by Google. In my Angular 9 application, I utilize Angular Google Maps to showcase a Google Map. However, as I implement this feature, I encounte ...

When using a Jquery $.each loop with DOM, it may not function properly on every element

I am attempting to create an HTML editor with additional markup capabilities in this demonstration, I am utilizing a jQuery $.each loop however, this loop only functions on the final element Here is the code snippet I am using: // Custom Svg Icon ...

How to trigger a jQuery function once a v-for loop has completed in VueJS?

Utilizing vue-resource, I successfully retrieve data from my API and incorporate it into my HTML. However, I am encountering an issue where I want to execute a jQuery function after the v-for loop has completed in order for jQuery to recognize elements in ...

Troubleshooting EJS Relative Path Problem when Using "include" in an HTML Document

I'm encountering an issue with my "index.ejs" file... The current content of the ejs file: <!DOCTYPE html> <html lang="en" dir="ltr"> <!-- THIS SECTION IS FOR <head> TAG THAT WILL BE STORED INSIDE "so_ ...

An error has occurred with the Firefox Addon: the module `path` cannot be located within the resource://gre/modules/commonjs/http.js

Currently developing a Firefox add-on on Windows10 with node v5.8.0 and npm v3.5.3, using Firefox v.45.0 The issue arises from the following line of code: var path = require("path"); The error message reads: Message: Module `http` is not found at resou ...

Rendering data from the server using backbone.js: A comprehensive guide

Initially, this is the response received from the server $response = array(); $i = 0; while($result = mysql_fetch_assoc($query)){ $response[$i]["id"] = $result["ID"]; $response[$i]["post_ ...

What is the most efficient way to retrieve the key at a specific index within a JavaScript map object?

If I have the map object shown below: const items = new Map([['item1','A'], ['item2','B'], ['item3', 'C']]) I am trying to retrieve the key at index 2. Is there a method other than using a for ...

Attempting to insert a square-shaped div within a larger square-shaped div and enable it to be moved around by dragging

Imagine this scenario: I have a button and a large div. When the button is clicked, the code adds a new div inside the larger one. However, the new div is not draggable because I didn't implement the necessary code. I'm also trying to figure out ...

The state update does not seem to be affecting the DOM

I've implemented this component in my React app. It updates the state every second, but oddly enough the value <p>{this.state.time}</p> is not changing as expected. When I print the state value, it does change every second. import React f ...

Using the DatePicker component with non-escaped Latin alphabet characters in date-fns for ReactJS

While attempting to integrate the DatePicker component from Material UI into my React project, I encountered an error. Although many attributed the issue to a version discrepancy, what ultimately resolved the problem for me was assigning a value to the Da ...

Top tip for receiving a JSON response object using AngularJS

I am currently working with an API that returns a json array object. In my Controller, I have been able to retrieve the json data successfully using the following code: angular.module('lyricsApp', []) .controller('LyricsController', [& ...

Is there a way to arrange list items to resemble a stack?

Typically, when floating HTML elements they flow from left to right and wrap to the next line if the container width is exceeded. I'm wondering if there's a way to make them float at the bottom instead. This means the elements would stack upward ...

What causes a TypeError (Invalid response status code) when a 204 response is returned to a fetch() call within a React Server Component?

Take a look at this straightforward Next.js application: https://codesandbox.io/p/sandbox/next-js-app-router-1bvd7d?file=README.md Here is an API route (/api/hello): export default function handler(req, res) { res.status(204).end(); } And there's ...