[英]Iterate through nested array object
I'm trying to iterate through a nested array object like below.我正在尝试遍历嵌套数组 object ,如下所示。 What is the best way to access each of the object elements within the nested arrays.
访问嵌套 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"
}
]
}
]
}
I need to access each of the elements in data and the dates array as well我还需要访问数据和日期数组中的每个元素
if I understand the question correctly you want to iterate over the dates array inside of each item in the data item, this is how I would do it in js如果我正确理解了你想要迭代数据项中每个项目内的日期数组的问题,这就是我在 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.