Retrieving string-based JSON information

Within my text box, the user inputs strings separated by commas. These strings are split on the front end, then sent to the backend to retrieve data in JSON format.

The interesting part is that when I directly entered the key of the JSON, like this, it worked:

var price = fun.results.KO;

However, when I attempted to use the value from the split list, it consistently returned an error:

list_of_key = ["KO", "OK", "NA"]
fun.results.list_of_key[1];

The error message displayed was: Uncaught TypeError: Cannot read property '0' of undefined.

Where did I go wrong? How can I rectify this issue?

If this were Python, it would be a whole different scenario.

Answer №1

To achieve the desired functionality, you must use square bracket notation.

fun.results[list_of_key[1]];

The property .list_of_key is not present within fun.results.

Answer №2

It seems like the solution you're looking for is...

fun.results[list_of_key[1]];

Instead of using fun.results.list_of_key[1]; the correct syntax should be to access the property list_of_key within fun.results. Attempting to index it directly results in the error you encountered.

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

Update the current value in array.prototype

Recently, I've noticed that the built-in JavaScript sort function can be unreliable at times. To address this issue, I decided to create my own sorting function. Consider the following scenario: Array.prototype.customSort = function(sortFunction, upd ...

Selenium - How to pass a file path to a dynamically generated input element that is not visible in the DOM

Check out this example of HTML code: This is how you create a visible button and display the selected file: <button id="visible-btn">visible button</button> <p>selected file is: <span id="selected-file"></spa ...

Combining and grouping objects by their IDs in a JavaScript array

Information: [ { "id": "ewq123", "name": "Joshua", "order": "Pizza" }, { "id": "ewq123", "name": "Joshua", "order": ...

Is it possible to pass multiple API props to a NextJs Page at once?

I am currently facing a challenge in rendering a page that requires data from two different API fetches. The URL in the address bar appears as: http://localhost:3000/startpage?id=1 Below is the code snippet for the first API fetch: import { useRouter } f ...

Managing route rendering in nuxtjs: A guide

I came across Goldpage, a tool that allows for route rendering control. Render Control - With Goldpage, you have the flexibility to choose how and when your pages are rendered. For example, one page can be rendered to both HTML and the DOM (classic serv ...

What steps can I take to troubleshoot the cause of my browser freezing when I try to navigate to a different webpage

Within this div, users can click on the following code: <div id="generate2_pos" onclick="damperdesign_payload();" class="button">Generate P-Spectra</div> Upon clicking, the damperdesign_payload() function is triggered, leading to a link to an ...

Is it possible for me to identify the original state of a checkbox when the page first loaded, or the value it was reset to when `reset()` was

When a webpage is loaded, various input elements can be initialized as they are declared in the HTML. If the user modifies some of the input values and then resets the form using reset(), the form goes back to its initially loaded state. I'm curious, ...

Dynamic anime-js hover animation flickering at high speeds

I have implemented the anime-js animation library to create an effect where a div grows when hovered over and shrinks when moving the mouse away. You can find the documentation for this library here: The animation works perfectly if you move slowly, allow ...

Determine the frequency of matching elements between an array and a JSON column by comparing the two and counting the number of similar terms

My goal is to develop a basic search engine that can determine the count of properties matching a user input. The data will be stored in JSON format with an array structure, allowing users to perform searches based on their preferences. The comparison pr ...

Concentrate on elements that are activated without the need for clicking

Is it possible to trigger an action when an input is focused without the focus event being initiated by a click? $('#input').focus(function(){ if(not triggered by click) { alert('Hello!'); } }); ...

Using Node JS, how to pass a variable length array to a function?

Is there a way to dynamically call an addon function with varying argument lengths? I capture user input in a variable like this: Uinput = [5,3,2]; Now, I want to pass these numbers as arguments to my addon function like this: addon.myaddon(5,3,2); I n ...

Do JavaScript functions operate synchronously or asynchronously?

Here is the JS code snippet I am working with: <script> first(); second(); </script> I need to ensure that second() will only be executed after first() has completed its execution. Is this the default behavior or do I need to make any modific ...

Tips on transferring values from script to controller in PHP using Laravel (for beginners)

I am trying to update the value in a textbox using a script and then save it in my database through my controller. Although the value in the textbox changes, the ajax call does not work as expected. I apologize for any mistakes, as I am still new to this p ...

Transition the object in smoothly after the slide has changed

How can I make the h4 tags fade in after the divs slide into view, while also adding the class "current" to each visible slide? Check out the example on JSFiddle here. <div class="slider"> <div class="slides"> <div class="slide ...

What is the best way to instruct jQuery to disregard an empty server response?

After examining this piece of code: $.ajax({ type: "POST", url: theRightUrl, data: whatToPost, logFunction: whatever, suppressSuccessLogging: !0, dataType: "html" }); I encountered an issue where Firefox displays a "no element ...

Combining JSON objects using a LEFT JOIN in SQL Server based on a shared property

In my database, I have two tables. One table holds the schema (key, type) of a JSON object, and the other table holds instances of objects based on that schema. Sometimes, the object instance may not include all the properties defined in the JSON schema, ...

Encountering Vue linting errors related to the defineEmits function

I am encountering an issue with the linting of my Vue SPA. I am using the defineEmits function from the script setup syntactic sugar (https://v3.vuejs.org/api/sfc-script-setup.html). The error messages are perplexing, and I am seeking assistance on how to ...

python: understanding the behavior of json.dumps with dictionaries

My goal is to modify the behavior of the dict when using json.dumps. I want to be able to order the keys, so I created a new class that inherits from dict and overrides some of its methods. import json class A(dict): def __iter__(self): for i ...

Leverage SwiftyJSON for parsing NSDate objects

How can a JSON date be efficiently deserialized into an NSDate object using SwiftyJSON? Should one utilize stringValue in conjunction with NSDateFormatter or is there an existing date API method within SwiftyJSON for this purpose? ...

Tips for enlarging the box width using animation

How can I create an animation that increases the width of the right side of a box from 20px to 80px by 60px when hovered over? What would be the jQuery code for achieving this effect? ...