Numbered keys in JSON

I'm dealing with a JSON object that is passed to a script, and the keys are dynamic which makes it hard for me to access the data.

The keys in the JSON object are actually numbers, but no matter how I try to access them, I can't seem to get the desired results.

var countries = {"223":"142,143","222":"23,26,25,24","170":"1,2"};

Initially, I attempted to access the data using the number as a key:

var objKey = 223;  (var objKey = "223";)
countries.objKey;

After that didn't work, I modified the JSON to have named keys instead:

var countries = {"country223":"142,143","country222":"23,26,25,24","country170":"1,2"};

Even after changing the keys, I still couldn't access the data successfully:

var objKey = "country"+223; (var objKey = "country"+"223";)
countries.objKey;

If anyone has any suggestions or advice on how to properly access this data, I would greatly appreciate it.

Answer №1

Try this alternative approach:

objectKey[countries];

By using square brackets, you can dynamically reference a property name by utilizing a variable (or even a string or number).

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

What is the best way to export multiple modules/namespaces with the same name from various files in typescript within index.d.ts?

I am currently in the process of creating a new npm package. I have two TypeScript files, each containing namespaces and modules with the same name 'X'. At the end of each file, I declared the following: export default X; My goal is to import bo ...

The customized tree component from Material UI is experiencing issues when displaying database data, however, it is functioning properly when manually

I am using React.js for the frontend and Django for the backend. I have data in a tree structure from the database that I want to display in a Material-UI Tree view component. Although I can log the data successfully, it is not rendering as a tree. React. ...

Showing Picture after Flask and AJAX manipulations

Currently, I am working on a project that involves implementing an explainable AI library for CNN image recognition (https://github.com/albermax/innvestigate) into a web-service. I stumbled upon a visually appealing template that focused on general image r ...

Incorporate one JSON object as a nested child within another using Java programming

I'm currently tackling a complex issue and need some guidance specifically with the following JSON array: parentArray: [{"name":"folder1","children":[],"parent":"root","type":"folder"}, {"name":"folder2","children":[],"parent":"folder1","type" ...

XPages: create JSON data on the server, utilize on the client-side

Struggling to overcome this seemingly simple hurdle, I find myself puzzled on how to proceed. My goal is to create a JSON structure on the server using ArrayObject and ObjectObject objects, and utilize it as a datasource both on the server-side (which work ...

Issue in CakePHP after modifying the directory of Index

I'm currently working on a project using CakePHP and have come across an issue. Our team developed an Ajax function that sends data to a PHP function responsible for adding a folder (known as "ordner" in German) to the database. Initially, everything ...

The modal window displays an error upon submission and prevents itself from closing

Below is the HTML code for a modal form: <form action="" class="cd-form" method="POST" id="signinform" onsubmit="return: false;"> <p class="fieldset" id="warningPanel"> <?php ShowAlertWarning(); ?> </p> <p ...

Ways to gradually reveal all elements within a header section

Is there a way to gradually reveal all the components in my header once the window has finished loading by utilizing jQuery's fadeIn function? Below is the pertinent code snippet: HTML <div class="banner"> <header class="header"> < ...

How can I use jQuery to pinpoint where my focus currently lies and debug any issues?

Triggering focus() on an element is a simple task, but after encountering debugging problems, I've come to realize that finding out where my focus has shifted can be quite challenging. The issue arises when I'm creating a modal window using jq.UI ...

transforming an array-containing string into a JSON format object

When making SOAPUI requests, the responses are in string format, regardless of their content. At time t, a request returns the following string: {satisfied=true, dynamic_href/properties/triggers, name=triggers, value={satisfied=true, dynamic_href/properti ...

JSON encoding malfunctioning

Can someone help me troubleshoot my JSON encoding code? Currently, when I run it, the result is an empty array []. I'm not sure where I've gone wrong, so I would appreciate any guidance. <?php $conn = new mysqli('localhost','ro ...

Importing ES module into Next.js leads to ERR_REQUIRE_ESM

Encountered this issue while attempting to integrate ky into a Next.js project: Error [ERR_REQUIRE_ESM]: Must use import to load ES Module: /foo/node_modules/ky/index.js It seems that the cause of this problem is Webpack (or Babel) converting all import ...

When using Sequelize.or, only the first result is returned instead of returning all results

Currently, I am integrating SequelizeJS(MySql) with Passportjs for authentication. When I try the following code: User.find(db.Sequelize.or({ 'username': username }, { 'email': req.body.email }) ) .then((user) => {consol ...

Unable to return data to HTML page

I'm utilizing EJS to pass some data to my webpage. Here's the snippet of code: app.post('/ttt', function (req,res){ res.render('index.ejs', {titles: 'CAME IN'}) }); HTML <form id="mc-form" action="http://loc ...

Creating a visually dynamic stack of images using javascript, jquery, and HTML

I'm interested in creating a unique image viewer using javascript/jQuery/HTML that combines elements of a book page flip and iTunes coverflow, optimized for mobile device browsers. I've been searching for tutorials to help kickstart this project, ...

Checking for the accuracy of the provided full name

There is a specific task at hand: The field labeled “First Name Last Name” must only contain 2 words, with each word being between 3 and 30 characters in length. Additionally, there should be only one space between the first and last name. The issue t ...

Utilizing ng-model to control the visibility of a label or anchor tag

Here is the code snippet I am working with: <div class="tabs DeliveryRightDiv"> <label class="selected"><a>One</a></label> <label> <a>Two</a> </label> <label> ...

Using Slick.JS to Sync Navigation with Main Slider, Automatically Resetting to First Slide

I have a question about the functionality of the Slick.JS plugin that I'm using. In my project, I have two carousels with five slides each. The top carousel displays one slide at a time, while the bottom carousel shows all five slides concurrently. My ...

Three.js Object failing to receive lighting effects

In my scene, I have an Object loaded with MeshBasicMaterial and it looks fine. However, as soon as I switch to MeshLambertMaterial, the object becomes completely dark. I've already set up an ambient light, a point light, and a box next to the Object. ...

Expanding a JSON type in Typescript to accommodate interfaces

Expanding on discussions about passing interfaces to functions expecting JSON types in Typescript, this question delves into the complexities surrounding JSON TypeScript type. The scenario involves a JSONValue type that encompasses various data types like ...