[英]Iterate through nested array object
我正在嘗試遍歷嵌套數組 object ,如下所示。 訪問嵌套 arrays 中每個 object 元素的最佳方法是什么。
{
"titleId": "111G",
"aNumber": "1212",
"data": [{
"id": "6657",
"name": "test name",
"city": "LA",
"state": "CA",
"comment": "comment 1",
"dates": [{
"startDate": "01/17/2020",
"endDate": "01/22/2020"
},
{
"startDate": "01/24/2020",
"endDate": "01/30/2020"
}
]
},
{
"id": "123",
"name": "abc",
"city": "NJ",
"state": "NY",
"comment": "comment 2",
"dates": [{
"startDate": "01/17/2020",
"endDate": "01/22/2020"
},
{
"startDate": "01/24/2020",
"endDate": "01/30/2020"
}
]
}
]
}
我還需要訪問數據和日期數組中的每個元素
如果我正確理解了你想要迭代數據項中每個項目內的日期數組的問題,這就是我在 js 中的做法
var date = JSON.parse(res.data)
date.forEach(element => {
var items = element.dates
items.forEach(current => {
//do whatever
});
});
const info = {
"titleId": "111G",
"aNumber": "1212",
"data": [{
"id": "6657",
"name": "test name",
"city": "LA",
"state": "CA",
"comment": "comment 1",
"dates": [{
"startDate": "01/17/2020",
"endDate": "01/22/2020"
},
{
"startDate": "01/24/2020",
"endDate": "01/30/2020"
}
]
},
{
"id": "123",
"name": "abc",
"city": "NJ",
"state": "NY",
"comment": "comment 2",
"dates": [{
"startDate": "01/17/2020",
"endDate": "01/22/2020"
},
{
"startDate": "01/24/2020",
"endDate": "01/30/2020"
}
]
}
]
}
info.data.forEach(city => city.dates.forEach(cityDate => console.log(cityDate.startDate)))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.