【发布时间】:2019-09-11 09:09:14
【问题描述】:
我需要在我的 fetch promise 中返回一个包含我所有数据的 JSON 对象。问题是我不知道对象名称是什么。我所知道的是,总会有一个对象。
这是我的示例代码,我在其中得到了我需要知道对象名称的内容(在本例中为 foo
return fetch(endPoint)
.then(res => res.json())
.then(res => res.foo)
.then(res => console.log(res))
我的回复是这样的
{
"foo": [
"bar1",
"bar2",
"bar3"
]
}
但是,如果这是响应,我的代码将失败:
{
"goo": [
"bar1",
"bar2",
"bar3"
]
}
无论调用什么对象,我如何确保我的代码始终有效?
【问题讨论】:
标签: javascript arrays json object ecmascript-6