I'm relatively new to delving into the realm of JavaScript and JSON, and finding it quite challenging. My current struggle involves accessing and parsing a JSON file stored on my web server into a JavaScript object. What I aim to accomplish is parsing the JSON data into an array and then manipulating that array based on various user inputs.
Here's a snippet of what the JSON data looks like:
{"log":
[{
"name":"Al",
"entries":[8,12,16,19]},
{"name":"Steve",
"entries":[11,17,22]}]}
The task at hand is to extract one of the entry arrays and store it as a JavaScript object in an array format. Here's the approach I've taken so far:
var entriesLogged;
fetch ('url to the json file').then(function(response){
return response.json();
}).then(function(data){
entriesLogged = data.log[0].entries;
});
Unfortunately, I've been unable to make this work effectively and persistently assign the value to the variable beyond its scope. While I was able to log the array values using console.log, manipulating the data as an object proved to be more challenging. Ideally, I would like to parse the JSON file from the server directly into a global array.
Most of the tutorials I've consulted thus far focused on logging or displaying JSON data on HTML elements, whereas my focus lies on storing these values to a global array first.
Any insights or advice on how to achieve this would be greatly appreciated.
Warm regards, Dom