-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata-handler.js
More file actions
34 lines (27 loc) · 912 Bytes
/
Copy pathdata-handler.js
File metadata and controls
34 lines (27 loc) · 912 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
let defaultFolder = "data/";
let root = "./";
// TODO add browser caching
// Loads a file from givern folder.
// filename - without .json.
// first key in the json file should be the filename. Can be overriden by key.
async function loadFile(filename, folder = defaultFolder, key = '') {
const filePath = root + folder + filename + '.json';
console.log("Loading JSON data from: " + filePath);
try {
//console.log(location.hostname);
const response = await fetch(filePath);
if (!response.ok)
throw new Error('Error loading JSON: ' + response.status);
const data = await response.json();
if (!key)
key = filename;
// Process the data and store it in an array
const dataArray = data[key];
//console.log(data);
return dataArray;
// Continue working with the array as needed
} catch (error) {
console.error(error);
}
}
export { loadFile };