Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

reference a number field/title in JSON array

I am searching a json array full of google fonts. The fonts are organized by family -> files – >filename. The issue I am having is that the filename is sometimes stored as a number. For example (see bottom of array under "files" titled "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"

I can call the font file "regular" while iterating the json array, but I am not sure how to refer to the file labelled as "100" since I know numbers cannot be 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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You can use the bracket notation – files['$100'], which accepts a string as the key parameter.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading