[英]Iterate imported JSON object
I have problem with iterate of JSON imported object我对导入 object 的 JSON 的迭代有问题
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这是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.您可以在此实时示例中看到此行为,其中将默认属性添加到 object。
In order to not get a default
entry in the object, import from JSON files using this syntax:为了不在 object 中获得default
条目,请使用以下语法从 JSON 文件导入:
import countries from "all-countries-and-cities-json";
It works in Node.js, I don't know about other runtimes.它适用于 Node.js,我不知道其他运行时。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.