如何使用typescript循环通过JSON对象
我使用角度7。 我的查询返回具有以下格式的json数据:如何使用typescript循环通过JSON对象,json,typescript,Json,Typescript,我使用角度7。 我的查询返回具有以下格式的json数据: [ { "text": "test 1", "value": "1", "nbr": "1", "children": [ { "text": "test 1_1", "value": "1_1", "nbr": "2",
[
{
"text": "test 1",
"value": "1",
"nbr": "1",
"children": [
{
"text": "test 1_1",
"value": "1_1",
"nbr": "2",
"children": [
{
"text": "test 1_1_1",
"value": "1_1_1",
"nbr": "1",
"children": []
},
{
"text": "test 1_1_2"",
"value": "1_1_2",
"nbr": "0",
"children": []
},
{
"text": "test 1_1_3"",
"value": "1_1_3",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 1_2",
"value": "1_2",
"nbr": "0",
"children": []
}
]
},
{
"text": "test 2",
"value": "2",
"nbr": "0",
"children": []
}
]
我想循环这个数据,基本上循环子数据
this.httpservice.query({
}).subscribe((res: HttpResponse<TestEntity[]>) => {
this.temp= res.body;
this.temp.forEach((x) => {
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
})
})
});
});
我想做一些测试
我尝试使用这段代码,它有循环子数据的问题
this.httpservice.query({
}).subscribe((res: HttpResponse<TestEntity[]>) => {
this.temp= res.body;
this.temp.forEach((x) => {
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
x["children"].forEach(x => {
if(x.nbr=='0')
{
// test code
}
})
})
});
});
this.httpservice.query({
}).订阅((res:HttpResponse)=>{
该温度=res.body;
每小时的温度((x)=>{
x[“儿童”]。forEach(x=>{
如果(x.nbr=='0')
{
//测试代码
}
x[“儿童”]。forEach(x=>{
如果(x.nbr=='0')
{
//测试代码
}
})
})
});
});
我没有找到循环儿童数据的方法
任何帮助都将不胜感激应该是:
const objects = Object.keys(data).map(key => data[key]);
应该是这样的:
const objects = Object.keys(data).map(key => data[key]);