JavaScript 过滤多层数据结构
let promotGroups = [ {
"promotionId":"1111111111",
"promotionName":"",
"promotionDescription":"",
"havePromotion":false,
"classes":[
{
"classId":"77777777777777",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
},
{
"classId":"1111111111111111111",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
}
]
},
{
"promotionId":"2222222222",
"promotionName":"",
"promotionDescription":"",
"havePromotion":false,
"classes":[
{
"classId":"6e64da1f52694f608293267c87865ce3",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
},
{
"classId":"000000000",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
}
]
}
]
我想要过滤的结果还是上面的那样 只是不同的 classes 取值不一致?
请问一下 怎么过滤??
理想的结构:
let new = [ {
"id":"1111111111",
"newarr":[
{
"id":"77777777777777",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
},
{
"classId":"1111111111111111111",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
}
]
},
{
"id":"2222222222222",
"newarr":[
{
"id":"00000000000000",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
"goodsType":0,
},
{
"classId":"1111111111111111111",
"tutorPictureUrl":"",
"teachers":[
{
"teacherId":"bf2ab5882d9e470a825eee97541d8868",
"teacherName":"张亚飞测试",
"name":"张亚飞测试",
"realName":"张亚飞测试",
"sex":0,
"pictureUrl":""
}
],
}
]
},
]
回答:
//多层map能返回你想要的结果
promotGroups.map(item => {
return {
id: item.promotionId, newarr: item.classes.map(val => {
return {id:val.classId,goodsType:val.goodsType,teachers:val.teachers}
})
}
})
回答:
js map 循环 return
以上是 JavaScript 过滤多层数据结构 的全部内容, 来源链接: utcz.com/p/937655.html