Javascript 如何在DOM上显示嵌套数据?

Javascript 如何在DOM上显示嵌套数据?,javascript,arrays,json,for-loop,axios,Javascript,Arrays,Json,For Loop,Axios,尝试从API获取数据并将其添加到DOM 特别是包含对象的数组 下面是API在控制台中返回内容的示例 我使用for循环和for…in循环来访问对象中的数组 代码如下 const getNews=document.getElementById('btn') heyThere=()=>{ axios.get()https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=APIKEY') .然后(功能(响应){ 对

尝试从API获取数据并将其添加到DOM

特别是包含对象的数组

下面是API在控制台中返回内容的示例

我使用for循环和for…in循环来访问对象中的数组

代码如下

const getNews=document.getElementById('btn')
heyThere=()=>{
axios.get()https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=APIKEY')
.然后(功能(响应){

对于(设i=0;i您需要访问要显示的
response.data.articles[i]
的特定属性,并为每个属性创建所需的HTML。例如:

const getNews=document.getElementById('btn')
heyThere=()=>{
axios.get()https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=APIKEY')
.然后(功能(响应){
让html='';
response.data.articles.each(article=>{
html+='';
html+=`${article.author}`;
html+=`${article.description}`;
html+='';
});
ham.innerHTML=html;
console.log(响应)
console.log(响应类型)
})
.catch(函数(错误){
console.log(错误);
})
}

getNews.addEventListener('click',heyThere)
下面的解决方案将文章作为列表打印到DOM中

heyThere=()=>{
axios.get()https://newsapi.org/v2/top-headlines?country=us&category=business&apiKey=APIKEY')
.然后(功能(响应){
让新闻=response.data.articles
for(设i=0,len=news.length;i