I have problem with iterate of JSON imported object
import * as countries from "all-countries-and-cities-json";
// obj with 152 keys, looks like:
//
// {
// "Afghanistan": [
// "Herat",
// "Kabul",
// "Kandahar",
// "Molah",
// "Rana",
// "Shar",
// "Sharif",
// "Wazir Akbar Khan"
// ],
// "Albania": [
// "Elbasan"...
console.log(Object.keys(countries).length); // return 153 (152 'default')
console.log(Object.keys(countries).pop()); // return 'default'
How to import this without key "default"?
Here is tsconfig.json
{
"compilerOptions": {
"esModuleInterop": true,
"resolveJsonModule": true,
}
}
You can see this behavior in this live example where a default property is added to the object.
CodePudding user response:
In order to not get a default
entry in the object, import from JSON files using this syntax:
import countries from "all-countries-and-cities-json";
It works in Node.js, I don't know about other runtimes.