javascript - 从 Fetch 中获取 Json 数据
问题描述
我的 Json 文件是一个带有 2 个数组的简单对象:
{
"key1": ["a", "b", "c"],
"key2": [ "d", "e", "f"]
}
在 Javascript 中,我使用 fetch 来获取数据。要使用数据,我将其推入数组
const jsonData = '/things.json';
const myArray = [];
fetch(jsonData)
.then(answer => answer.json())
.then(data => myArray.push(data))
console.log(myArray); //this gives me a nested array
console.log(myArray[0]); //undefined
将 myArray 通过循环也给了我一个 //undefined
使用扩展语法或 concat 给我 //Found non-callable @@iterator
如何获取该数组的内容?
解决方案
推荐阅读
- html - 使用 flexbox 保持容器居中并在调整大小时缩小
- laravel - Laravel Spark 中的图标
- yaml - 基于 _data 文件的条件 Jekyll Liquid 类
- ios - 泛型 Objective-C 类的扩展无法在运行时访问该类的泛型参数
- featherlight.js - 关闭后无法重新打开featherlight div
- java - java.io.IOException:尝试读取 aws s3 存储桶上的已关闭流
- javascript - 无法解析 'lodash/fp - 谷歌地图
- java - Guice 中的子注射器会自动创建吗?
- java - 带有自定义 DataPackage 的 Java NotSerializableException
- c# - TableLayoutPanel 运行时添加自动调整大小的列