Home > Software engineering >  Calling all directories in JSON
Calling all directories in JSON

Time:04-16

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"
  },

CodePudding user response:

// 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);

CodePudding user response:

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"
    },
    ...
};
  • Related