js 遍历数组对象 根据属性值 生成新的数组对象
[{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00:00.0",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00",
"teacherName": "老师",
"drillID": 0,
"sectionId": 145,
"sectionLevel": "3",
"parentId": 131,
"content": "章节看到了吗",
"sectionName": "疾病类型",
"courseDrillId": 10,
"userSectionStats": 0,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 131,
"sectionLevel": "2",
"parentId": 55,
"content": "day1内容",
"sectionName": "day1",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 55,
"sectionLevel": "1",
"parentId": 0,
"content": "结合流行病学史、临床表现",
"sectionName": "诊断",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
},
{
"clockInState": 0,
"sectionNum": "2",
"courseBegin": "2019-02-09 00:00:00.0",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "14",
"courseBegin": "2019-02-22 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"sectionName": "打吊针",
"clockInState": 0,
"sectionNum": "1",
"courseDrillId": 10,
"courseBegin": "2019-02-22 00:00",
"teacherName": "老师",
"drillID": 0,
"userSectionStats": 0,
"sectionId": 210,
"sectionLevel": "3",
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg",
"parentId": 209
}
],
"sectionId": 209,
"sectionLevel": "2",
"parentId": 207,
"sectionName": "day1",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
},
{
"clockInState": 0,
"sectionNum": "15",
"courseBegin": "2019-02-23 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"sectionName": "吃西药",
"clockInState": 0,
"sectionNum": "1",
"courseDrillId": 10,
"courseBegin": "2019-02-23 00:00",
"teacherName": "老师",
"drillID": 0,
"userSectionStats": 0,
"sectionId": 402,
"sectionLevel": "3",
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg",
"parentId": 236
}
],
"sectionId": 236,
"sectionLevel": "2",
"parentId": 207,
"sectionName": "day2",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 207,
"sectionLevel": "1",
"parentId": 0,
"content": "undefined",
"sectionName": "治疗",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
]
有这样一个结构的数组对象 和 如下结构的一维数组
const teachers = ['张三', '李四' …]
现在希望遍历数组对象,通过比对 teacherName 属性是否在 teachers 中存在,得到一个新的数组对象
遍历方法怎么写 ?
回答:
// 用js的filter方法,然后返回精准的数据list.filter((item)=>{return item.teacherName === teaName})
// teaName 是用来做数据过滤的。// 精准匹配:只有名字强等才会返回对应的数据信息
list.filter((item)=>{return item.teacherName === teaName})
// 模糊匹配:只要 teacherName包含对应的字符就会返回数据
list.filter((item)=>{return item.teacherName.indexOf(teaName) !== -1})
回答:
var list = [{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00:00.0",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00",
"teacherName": "张三",
"drillID": 0,
"sectionId": 145,
"sectionLevel": "3",
"parentId": 131,
"content": "章节看到了吗",
"sectionName": "疾病类型",
"courseDrillId": 10,
"userSectionStats": 0,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 131,
"sectionLevel": "2",
"parentId": 55,
"content": "day1内容",
"sectionName": "day1",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 55,
"sectionLevel": "1",
"parentId": 0,
"content": "结合流行病学史、临床表现",
"sectionName": "诊断",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
},
{
"clockInState": 0,
"sectionNum": "2",
"courseBegin": "2019-02-09 00:00:00.0",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"clockInState": 0,
"sectionNum": "14",
"courseBegin": "2019-02-22 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"sectionName": "打吊针",
"clockInState": 0,
"sectionNum": "1",
"courseDrillId": 10,
"courseBegin": "2019-02-22 00:00",
"teacherName": "老师",
"drillID": 0,
"userSectionStats": 0,
"sectionId": 210,
"sectionLevel": "3",
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg",
"parentId": 209
}
],
"sectionId": 209,
"sectionLevel": "2",
"parentId": 207,
"sectionName": "day1",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
},
{
"clockInState": 0,
"sectionNum": "15",
"courseBegin": "2019-02-23 00:00",
"teacherName": "老师",
"drillID": 0,
"childList": [
{
"sectionName": "吃西药",
"clockInState": 0,
"sectionNum": "1",
"courseDrillId": 10,
"courseBegin": "2019-02-23 00:00",
"teacherName": "李四",
"drillID": 0,
"userSectionStats": 0,
"sectionId": 402,
"sectionLevel": "3",
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg",
"parentId": 236
}
],
"sectionId": 236,
"sectionLevel": "2",
"parentId": 207,
"sectionName": "day2",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
],
"sectionId": 207,
"sectionLevel": "1",
"parentId": 0,
"content": "undefined",
"sectionName": "治疗",
"courseDrillId": 10,
"userSectionStats": 1,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
}
];
var findTree = (list, teachers, acc=[])=>(list.forEach(item=>{
teachers.includes(item.teacherName) && acc.push(item);
findTree(item.childList ||[], teachers, acc);
}),acc);
var teachers = ['张三', '李四'];
findTree(list, teachers);
结果
[ {
"clockInState": 0,
"sectionNum": "1",
"courseBegin": "2019-02-09 00:00",
"teacherName": "张三",
"drillID": 0,
"sectionId": 145,
"sectionLevel": "3",
"parentId": 131,
"content": "章节看到了吗",
"sectionName": "疾病类型",
"courseDrillId": 10,
"userSectionStats": 0,
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg"
},
{
"sectionName": "吃西药",
"clockInState": 0,
"sectionNum": "1",
"courseDrillId": 10,
"courseBegin": "2019-02-23 00:00",
"teacherName": "李四",
"drillID": 0,
"userSectionStats": 0,
"sectionId": 402,
"sectionLevel": "3",
"courseCover": "courseCover/courseCover预防感冒1547003167675.jpg",
"parentId": 236
}
]
以上是 js 遍历数组对象 根据属性值 生成新的数组对象 的全部内容, 来源链接: utcz.com/p/935821.html