javascript - 循环遍历 JavaScript 对象和子对象
问题描述
我正在尝试遍历 API 的响应。到目前为止我的代码:
async function getStreams(gamename){
let url = `https://api.twitch.tv/kraken/streams/?game=${gamename}`;
const response = await fetch(url,{ method: 'GET', headers: headers});
const json = await response.json();
var test = Object.values(json)
});
测试变量返回一个像这样的对象。
原始响应看起来像这样。
由于我感兴趣的所有内容都在streams
我不知道如何分离每个流然后检查每个方面,例如channels.language
密钥。
我觉得我几乎拥有以下内容:
Object.values(json).forEach(function(key,index) {
// key: the name of the object key
// index: the ordinal position of the key within the object
console.log(key)
});
但是,现在我需要更深入地了解密钥——<code>key.values,key[0]
和任何类似的东西都行不通。我需要遍历这个指定键的所有内容并检查 if language === 'de'
,然后打印整个键/对象。由于language
标签位于其中的另一个子对象channel
中,因此我对使用什么感到困惑。
解决方案
分解问题。streams
是一个对象数组。您可以根据来自channel
.
const germanChannels = streams.filter(stream => stream.channel.language === 'de');
推荐阅读
- css - 如何将多个 css 文件和 javascript 文件编译成一个文件?
- go - 当你从一个频道读取并且你正在推送到另一个频道时,你如何编写一个不会挂起的函数
- android - androidx.appcompat.view.menu.ActionMenuItemView 无法转换为 android.view.MenuItem
- java - Java 内存堆调优
- marklogic - Marklogic ml-gradle 不清除数据库
- reactjs - 嵌套 Emotion 10 类级联
- android - StaggeredGridLayout,每行动态对象数,换行
- opengl - 如何确定调用 glTexImage3D() 的数据长度(以字节为单位)?
- html - 创建从条形图到相同条形图但具有不同列的转换时出错
- c++ - 为什么 std::bitset<5>{}[0] 不是 constexpr?