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
>Solution :
You can use the bracket notation – files['$100'], which accepts a string as the key parameter.