Creating JSON path with numerical tags in JavaScript

Javascript:

var json_obj = 
{
   "1111": {
       "name": Bob,
       "id": 1
   },
   "2222": {
       "name": Alice,
       "id": 2
   }
}
var first_name = json_obj.1111.name; // Encountering a missing ';' before statement error

This code snippet utilizes the json_obj from a larger json file within our project. I contemplated altering the json file to simplify element retrieval, however it is an extensive file that is heavily relied upon across different parts of the project. Can anyone offer guidance on how to efficiently locate elements in such scenarios?

Answer №1

If you encounter this situation, bracket notation can be utilized

var data = {
  "1234": {
    "title": "Example",
    "number": 1
  },
  "5678": {
    "title": "Sample",
    "number": 2
  }
}
var item_title = data['1234'].title;

document.write(item_title);

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

Nested Ajax calls within a loop

Here is the code structure I am working with: for (var x = 0; x < array1.length; x++){ (function(x) { $.ajax({ url : "http://someurl1.ru/"+array1[x]+"/", async: false, success : function(someresult1){ array2.length = 0; ...

Interactive radio button that only registers the most recent click

Homepage.jsx const Homepage = () => { const [categories, setCategories] = useState([]) const [products, setProducts] = useState([]) const [selected, setSelected] = useState("all") const getAllCategories = async() => { try{ ...

Angular JS is failing to update views when passing dynamic IDs

I have implemented a method in my controller where I pass a dynamic id using the ng-init function, fetch a response through ajax calls, and try to print the response. However, I am facing an issue as the response is not getting printed. I am using ng-repea ...

In TypeScript, maximize the capabilities of JavaScript modules by utilizing the import extension

Incorporating the dynamo-cache package from NPM into my TypeScript project has been a bit of a challenge. Essentially, this module introduces a new method to the AWS.DynamoDB.DocumentClient: AWS.DynamoDB.DocumentClient.prototype.configCache = function(con ...

What are the components found within literal entities?

Here is an example of HTML code: <div id='myDiv'></div> If I try to hide the 'myDiv' element using the following JavaScript, it doesn't work: var test = { vars: { $mydiv: $('#myDiv') }, ...

Encountering a ReferenceError stating that the window object is not defined while building a React Leaflet application

In my upcoming nextjs project, I've incorporated react-leaflet. It behaves flawlessly in dev mode. However, upon attempting to build the project, it throws a ReferenceError: window is not defined. Despite setting ssr to false in dynamic, the error per ...

The INSERT statement encountered a syntax error while trying to process JSON data

I am struggling to find the accurate syntax for storing JSON in a SQL table. The structure of the table is as follows, with the id incrementing automatically : https://i.sstatic.net/mnoDB.png When attempting to execute the query, I encounter an error ind ...

What exactly is the significance of the number 15 passed as the second parameter in the json_encode() function

While exploring Laravel Blade, I observed that the @json($list) directive from the official documentation (https://laravel.com/docs/7.x/blade) gets converted to: <?php echo json_encode($list, 15, 512) ?> I was left wondering about the significance ...

Trapping an iframe refresh event using jQuery

I am trying to dynamically load an iframe using jQuery. Here is my code: jQuery('<iframe id="myFrame" src="iframesrc.php"></iframe>').load(function(){ // do something when loaded }).prependTo("#myDiv"); Now, I need to capture th ...

Can you advise on the best approach to transform a flat dataframe into a nested JSON structure using Spark in either Scala or Java?

In my SQL query results, I have a dataset available in a dataframe structured like this: id,type,name,ppu,batter.id,batter.type,topping.id,topping.type 101,donut,cake,0_55,1001,Regular,5001,None 101,donut,cake,0_55,1002,Chocolate,5001,None 101,donut,cake, ...

A step-by-step guide on utilizing the "Add Object To JSON" function in Robot

Here is a JavaScript Object Notation (JSON) object I'm working with: [{'creationDatetime': '2022-07-01T15:03:45.928Z', 'eventIds': ['ff7f9780-f94e-11ec-93a8'], 'id': 1, 'info': 'API name ...

Troubleshooting npm start error: "Cannot find module: Error: Unable to resolve..." when relocating webpack/react files

Initially, my project was functioning perfectly and I could easily access my web page on localhost:8080 using Chrome. In an attempt to learn something new, I organized all my files into a folder named 'dir copy' instead of just 'dir'. O ...

Exploring intricate data structures with Hive

When trying to fetch data from the source table, I am facing difficulty in viewing the results properly. Description of Source Table c1 string, c2 string, c3 string, temp1 struct < s1 : string, s2 : string, s3 : string, temp2 : array<struct<as1 ...

Is it possible to perform a test and decrement simultaneously as an atomic operation?

After tracking down a frustrating bug, I have discovered that it is essentially a race condition. Let's consider a simple document structure for the purpose of this discussion, like { _id : 'XXX', amount : 100 }. There are hundreds of these ...

Experimenting with the testing of two distinct functions

Hello, I am new to the world of testing and I would appreciate some guidance. I have two functions that I need help with. The first function is a bits-per-second converter that converts data into a more readable format. const convertBitrate = bitrate =&g ...

A guide on how to view material.resolution in Three.js

I've been following a tutorial and I'm attempting to create a thick line using different coordinates. https://github.com/leighhalliday/google-maps-threejs/blob/main/pages/car.js But I've encountered an issue in my code: trackRef.current.mat ...

Navigating through the keys of a parameter that can assume one of three distinct interfaces in TypeScript: a guide

Here is a function example: function myFunc(input: A | B | C) { let key: keyof A | keyof B | keyof C; for(key in input) { let temp = input[key]; console.log(temp); } } The definitions for A, B, and C are as follows: interfa ...

Steps to convert a phone number into JSON format

The primary focus Upon receiving an MQTT packet, it is displayed as an ASCII array in the buffer after being printed using stringify: packet = { "cmd": "publish", "retain": true, "qos": 1, "dup& ...

The utilization of AJAX commands within the appgyver steroids platform is a game

After transferring my phonegap project to steroids, I am encountering issues with my ajax commands not functioning properly. I am completely stumped as to what could be causing this problem, so any suggestions would be greatly appreciated. The culprit see ...

What is the best way to extract and count specific values from a JSON file using JavaScript?

My JSON data looks like this: /api/v1/volumes: [ { "id": "vol1", "status": "UP", "sto": "sto1", "statusTime": 1558525963000, "resources": { "disk": 20000000 }, "used_resources": { "disk": 15000000 }, "las ...