I am having trouble retrieving the API data. How can I determine if my code is correct or incorrect?

Here is some API data retrieved from a website...

date: (...)
dateTimeGMT: (...)
matchStarted: (...)
squad: (...)
team-1: (...)
team-2: (...)
toss_winner_team: (...)
type: (...)
unique_id: (...)
winner_team: (...)

When I use console.log(response.date) or

console.log(response.toss_winner_team)
, the code works correctly. However, if I use console.log(response.team-1) or console.log(response.team-2), it returns NaN.

You can view the API data in the image provided below:

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

Answer №1

To retrieve the property named "team-1", use response["team-1"].

Attempting to access response.team-1 would result in treating it as a subtraction operation with the property value.

If the data within the property is an integer (response.team), you can subtract 1 from it using

console.log(parseInt(response.team)-1);

Answer №2

response.team-5

In JavaScript, it's important to use valid variable names. Instead of using team-5, consider accessing it as an array with a string key like response["team-5"].

Answer №3

When JavaScript wants to evaluate a specific expression, it's best to access it in array style:

console.log(response["team-1"])

If you're not sure what you're trying to accomplish, consider iterating over the response if it's an array:

response.forEach(row => console.log(row["team-1"]));

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

Using a TCP server to transfer and store files with node.js

I am managing multiple devices that are continuously sending messages to a TCP Server built in node. The primary purpose of this TCP server is to route certain messages to redis for further processing by another application. I have created a basic server ...

Show a distinct row from an API using React

I am attempting to create a map function to display all the items from the API Screenshot of code showing the items Here is the console log displaying the fetched items from the API I encountered an error with the map function not working. Any solutions ...

Loading strings into elements of a BYTE* array in C++: A step-by-step guide

I have a program that reads the contents of a file and converts it into hexadecimal format. I want to store these hex values as elements in an array named hexData, which is declared as unsigned char. Can someone provide guidance on how to achieve this? I ...

What is the best way to transform the response data received from axios into a different response format?

After accessing response.data, I am getting a result like:  [{…}, {…}] If I expand it, here is how it looks: [ 0: {...}, 1: {...} ] I have attempted various methods to extract the values, including using a for loop, but it results in an infinite ...

Updating view with *ngIf won't reflect change in property caused by route change

My custom select bar has a feature where products-header__select expands the list when clicked. To achieve this, I created the property expanded to track its current state. Using *ngIf, I toggle its visibility. The functionality works as expected when cli ...

Annotating Vue Computed Properties with TypeScript: A Step-by-Step Guide

My vue code looks like this: const chosenWallet = computed({ get() { return return wallet.value ? wallet.value!.name : null; }, set(newVal: WalletName) {} } An error is being thrown with the following message: TS2769: No overload ...

What is the best way to invoke JavaScript from C# code in Ext.NET?

Within my project, I encountered a situation where the displayed time in Chrome is incorrect while it appears correctly in Explorer. To address this issue, I wrote a JavaScript code, but unfortunately, it did not resolve the problem. Below is the JavaScrip ...

Executing actions on events in a Basic jQuery sliderLearn how to handle events and execute

I utilized the slider from the bjqs plugin at after reviewing the documentation, I noticed it only offers options and lacks events/functions. I am referring to events/functions like onSlideChange and onSlideDisplay. While other plugins such as bxslid ...

Move to Fieldset Upon Link Click

Here's an example of what I have implemented so far here It's evident that this is not fully functional due to the PHP and jQuery integration. This demo is just a showcase of my progress. I am looking to create a functionality where clicking on ...

What is the significance of the exclamation mark in Vue Property Decorator?

As I continue to explore the integration of TypeScript with Vue, I have encountered a query about the declaration found in the Vue property decorator documentation. @Prop({ default: 'default value' }) readonly propB!: string ...

Issue with sending POST and PUT requests using axios in combination with a vuetify datatable component in a vue.js application

I'm currently customizing a sample datatable from the vuetify website to suit my requirements by integrating axios to consume my API. The GET AND DELETE methods are functioning correctly, but I am facing challenges with the POST AND PUT methods. My se ...

Global variable in Npm CLI

I'm working on a CLI tool that heavily relies on storing a unique identifier (uid). I attempted to use fs to store the uid; however, the file was created in the directory where the command was executed. #!/usr/bin/env node const program = require("co ...

Is there a way to dynamically update the 'src' attribute of an iframe by clicking on a different page?

On my website, I have two html pages named 1.html and 2.html. In 1.html, there are two buttons that each link to different websites - Google and Facebook. One button directs to Google The other button directs to Facebook Within 2.html, I have an iframe ...

The importance of understanding Req.Body in NODE.JS POST Requests

Currently, I am developing a Node.JS application that interacts with a MySQL database. The app retrieves data from the database and displays it in a table using app.get, which functions correctly. The issue I am facing is that when utilizing app.post, re ...

Unable to retrieve the value stored in the global variable

I recently updated my code to use global variables for two select elements in order to simplify things. Previously, I had separate variables for values and innerHTML which felt redundant. Now, with global variables like user and group initialized at docum ...

Tips for persisting form values even after refreshing the page - a guide to setting form values that stay in place

When I submit a long form, an external JavaScript validation is triggered to check the input field validity. If all fields pass validation, a jQuery modal appears prompting the user to either register or log in. If the user chooses to register and complet ...

The router is displaying the component directly on the current page rather than opening it on a separate page

The issue is that the router is rendering the page on the same page instead of generating a new one. When clicking on the Polls link, it only renders the page there but the URL changes in the browser. Polls.js import React from 'react'; import ...

"Retrieving JSON data from a POST request made to an API with the use of Axios in a React application

Recently, I've been tasked with making a POST call to an API in a ReactJS UI upon clicking. The API is expected to return a series of JSON objects after the POST call. As someone who is new to React and axios, any guidance on how to capture these JSON ...

Tips for adjusting the color of boxes within a grid

I've built a grid containing multiple boxes, each identified with an id of box + i. However, I'm encountering difficulties when attempting to implement an on-click function to change the color of each box. Below is the code snippet in question: f ...

Java - Importing and handling positive and negative whole numbers from a text file

I'm encountering an issue while attempting to read in 10 signed integers from a file into an array. Strangely, the process isn't working as expected and I'm not receiving any errors during either compile or runtime. I'd really appreciat ...