'Calling all directories in JSON
I want to change below values ../data/data_0/ into a general syntax so that I don't have to specify those directories individually as I have to give 500 directories. I know some other languages just use * i.e. ../data/data_*/ but I guess this is not the case in JSON.
"training": {
"training_data": {
"systems": ["../data/data_0/", "../data/data_1/", "../data/data_2/"],
"batch_size": "auto",
"_comment": "that's all"
},
Solution 1:[1]
// suppose you have many directries i.e 500
const dirs = [...Array(500).keys()].map((dir) => `./data/data_${dir}`);
const result = {
training: {
training_data: {
systems: dirs,
batch_size: "auto",
_comment: "that's all"
}
}
};
console.log(result);
Solution 2:[2]
Use a for loop to create an array with all 500 directories, then put it into the object.
let systems = [];
for (let i = 0; i < 500; i++) {
systems.push(`../data/data_${i}`);
}
let object = {
"training" : {
"training_data": {
"systems": systems,
"batch_size": "auto",
"_comment": "that's all"
},
...
};
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Harsh Mangalam |
| Solution 2 | Barmar |
