我需要一些關于 JavaScript 回圈的幫助。請參閱下面的 JSON 資料
{
"blogs":{
"id1":{
"title":"Title 1",
"date":"test_date",
"datestamp":"test_datestamp 1",
"content":"The content",
"url":"https:\/\/www.testlink1.com",
"tags":["move","New"]
},
"id2":{
"title":"Title 2",
"date":"test_date",
"datestamp":"test_datestamp 2",
"content":"The content 2",
"url":"https:\/\/www.testlink2.com",
"tags":["Netherlands","Yellow"]
}
}
}
接下來我決議 JSON 如下
data = JSON.parse(this.response); //This JSON is the result from an AJAX call to a PHP file
為了使用資料,我這樣做
for(let id in data.blogs){
console.log(data.posts[id].date);
console.log(data.posts[id].title);
//etc.
}
但是我怎樣才能在這個回圈中回圈標簽陣列呢?
我試過這個但沒有結果
for(let id in data.blogs){
for(let tag in data.blogs.tags){
alert(data.blogs[id].tags[tag]);
}
}
誰能幫我解決這個問題?
uj5u.com熱心網友回復:
但是我怎樣才能在這個回圈中回圈標簽陣列呢?
您需要識別當前在流程回圈中的博客專案。
這里是適當的例子,注意for-in你寫的或者for-of回圈來回圈專案。
const data = {
blogs: {
id1: {
title: 'Title 1',
date: 'test_date',
datestamp: 'test_datestamp 1',
content: 'The content',
url: 'https://www.testlink1.com',
tags: ['move', 'New']
},
id2: {
title: 'Title 2',
date: 'test_date',
datestamp: 'test_datestamp 2',
content: 'The content 2',
url: 'https://www.testlink2.com',
tags: ['Netherlands', 'Yellow']
}
}
}
for (const blogId in data.blogs) {
const blogItem = data.blogs[blogId]
console.log(`looping ${blogItem.title}`)
for (const tag of blogItem.tags) {
console.log(tag)
}
}
for (const blogItem of Object.values(data.blogs)) {
console.log(`looping ${blogItem.title}`)
for (const tag of blogItem.tags) {
console.log(tag)
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/512450.html
上一篇:在擴展中更改innerHTML
下一篇:需要將物件轉換為JSON值
