0
let jsonFile = require('jsonfile')
let fileName = './data/characters.json'

var characterNames = [];

jsonFile.readFile(fileName, function(err, jsonData) {
    if (err) throw err;

    for(let i = 0; i < jsonData.length; i++) {
        characterNames[i] = jsonData[i].characterNames;
    }

  }
    
);
console.log(characterNames);

the terminal output is []

My JSON file is ok, and I try to output the array inside the callback function. I can see the result I want, but when I print the array outside, the result is empty. Why?

soupy-norman
  • 403
  • 2
  • 11

0 Answers0