mention a numerical value/heading within a JSON list

I encountered an issue while searching through a JSON array filled with Google fonts. The fonts are structured by family -> files -> filename. However, I noticed that sometimes the filename is saved as a number. For example (refer to the bottom of the array under "files" labeled "100"):

"family": "Roboto",
      "variants": [
        "100",
        "100italic",
        "300",
        "300italic",
        "regular",
        "italic",
        "500",
        "500italic",
        "700",
        "700italic",
        "900",
        "900italic"
      ],
      "subsets": [
        "cyrillic",
        "cyrillic-ext",
        "greek",
        "greek-ext",
        "latin",
        "latin-ext",
        "vietnamese"
      ],
      "version": "v30",
      "lastModified": "2022-09-22",
      "files": {
        "100": "http://fonts.gstatic.com/s/roboto/v30/KFOkCnqEu92Fr1MmgWxPKTM1K9nz.ttf",
"regular": "http://fonts.gstatic.com/s/roboto/v30/KFOmCnqEu92Fr1Me5WZLCzYlKw.ttf"

While iterating through the JSON array, I can easily reference the font file named "regular", but I am unsure how to access the file identified as "100" since numbers cannot be used as variables.

$.getJSON('https://www.googleapis.com/webfonts/v1/webfonts?key=mykey',    function(response) {
            var items = response.items;
            items.forEach(function(fontitem){
              if (fontitem.family == "Roboto") {
                  alert(fontitem.files.$100); //doesn't work
                                  alert(fontitem.files.regular); //works
                  return;
                  }
              });
          });
alert(fontitem.files.$100); //doesn't work while
alert(fontitem.files.regular); //works

Answer №1

A convenient way to access the key inside an object is by using bracket notation like this - files['$100']. This method allows you to use a string as the key parameter.

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 Express.js to send a response while simultaneously executing a background task

When working with Express.js, I have a need to execute a task after sending a response. My main goal is to minimize the response time and send back the response immediately without waiting for the task results to be returned to the client. The task itself ...

Simplified JavaScript Object Structure

A JSON array that is flat in structure looks like this: var flatObject = [ { id : "1", parentId : "0", name : "object 1" }, { id : "2", parentId : "1", name : "object 2" }, { id : "3", parentId : "2", name : "object 3" }, { id : "4", pare ...

Trouble arises when trying to define the style for an element generated through the Document

I'm having trouble setting the style for a 'tr' element created using DOM within JavaScript. Here's the code snippet: var tr = document.createElement("tr"); tr.onmouseover=function(){this.style.backgroundColor='#fbf9e0';}; ...

I am interested in sending an email from a PDF document based on the selected check boxes

I have added a button in a PDF form that says "send an email" and I would like the form to be sent to different email addresses based on which checkboxes were selected previously. For example, there is a question about the "Size of the company" with 2 che ...

Choosing a recently inserted row in jqGrid

After reloading the grid, I am trying to select the newly added row, which is always added at the end. However, it seems impossible to do so after the reload. Is there a reliable way to select the last row after reloading the grid? The current code I have ...

waiting for udp response in node.js using express

Currently, I am diving into the world of node.js programming and have encountered a challenge. While working with express, I came across an issue. When a POST request is made, it triggers a radius authentication process over UDP using the dgram module. Ho ...

Error in Javascript chrome when trying to determine the length of an array

I am facing an unusual issue with the JavaScript console in Chrome. When I type the following code into the console: var numbers = new Array(["/php/.svn/tmp", "/php/.svn/props"]); it returns "undefined." This leads me to believe that 'numbers' ...

Guide to generating an ndjson object using a dictionary in Python

Seeking assistance in creating an NDJSON object from data parsed from a prominent Advertising Platform, which will then be uploaded to bigquery. Although I successfully generated an NDJSON using pandas, issues arise with controlling datatypes, leading to ...

jQuery if statements not correctly functioning when using multiple conditions

Previously, this code was functioning correctly: if (item.isPrivateToSubOrg == false){ However, after adding this line of code, it stopped working: if (item.isPrivateToSubOrg == false && item.eventStatus == 'Published'){ I'm retr ...

Having difficulty adding multiple items to the state array

I am currently working on a parent component that iterates over an array and passes props to a child component. In the child component (shown below), I have checkboxes with Font Awesome icons for users to mark their selections. When a user checks a box, I ...

Upon clicking the submit button, retrieve all the table rows that have been selected

After populating a table with data from a CSV file, users have the ability to select specific rows. When they click submit, all selected row data is sent to a webservice. Although I have successfully implemented AngularJS(v1.6) to populate the data and re ...

Tips for bringing a StrongLoop API into a JSON or YAML format

I am utilizing an API that was created using loopback/strongloop and currently running smoothly. https://i.sstatic.net/Pyfcc.png My goal is to export this generated API in YAML or JSON format so that I can reuse it in another application. Specifically, I ...

Querying data in Postgresql 9.4 using the JSONB data type based on a specified date

Transitioning from Mongodb, I am exploring how to select by range using jsonb type. I have approximately 2,880,000 records per day and need to query the database based on station and date fields. While I understand how to select by time range: SELECT * FR ...

troubleshooting problems with jquery ajax and setInterval

$(document).ready(function() { function refreshBids() { var element = $("#biddingDiv"); element.load("bids.php?id=<?=$userId;?>&init=1&auctionid=<?=$auctionId;?>"+(new Date()).getTime()); } refreshBids(); setI ...

Is it possible for me to listen to an AngularJS event using regular JavaScript, outside of the Angular framework?

Is it possible to listen to an event triggered in AngularJS using regular JS (outside of Angular)? I have a scenario where an event is being emitted using RxJS in Angular 2. Can I observe that event from pure JS? Here's some example pseudo code: imp ...

Error: Module not located in Custom NPM UI Library catalog

I have recently developed an NPM package to store all of my UI components that I have created over the past few years. After uploading the package onto NPM, I encountered an issue when trying to use a button in another project. The error message "Module no ...

List out IP addresses in JavaScript

Is it possible to allow users to input a range of IP addresses in a specific format? 10.5.15.[22-25],10.5.16.[35-37],10.5.17.20 The desired outcome is to return an array of these IP addresses for connection checking purposes later on. 10.5.15.22 10.5.15 ...

Be warned: Babel has detected a duplicate plugin or preset error

Currently, I am enrolled in a React course on Frontend Masters. As part of the course, we were tasked with modifying the Babel config to allow state instantiations like: state = {index: 0} in class components. However, when I executed the command: npm i ...

How to neatly format a JSON object in Python 3.5

Struggling with pretty printing a JSON file? While many popular solutions like How to Python prettyprint a JSON file may not work, here is a solution that might help: The following code snippet shows how to properly load JSON data using Python: import js ...

The implementation of local JSON instead of external JSONP in Angular

I am exploring the option of storing a json-file on the same server as my Angular app. I am wondering about how I can modify this code to read from a locally stored json file: ergastAPI.getDrivers = function() { return $http({ method: 'GET&apos ...