使用filter过滤多层数据结构
dearweb 发布:2021-11-16 11:28:16阅读:在平时的工作中我们一般对数据过滤的处理比较多,而且大多是一层数组结构,今天主要介绍的是如何过滤多层数组结构,希望各位小伙伴看完收藏我!
数组结构代码
let courseTimeList = [ { id: 1, courseTimeOneList: [ { maxNumber: '30', price: '', teacherId: '5b7bbe50da9540fcb1d84e4e2f00fa8b', teacherName: '888999', theDate: '2021-11-19', theTimeBeginToEnd: '', index: [0, 3], unitPrice: '50', monthDayStr: '����', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, timeBeginEnd: '08:00 - 09:00', type: '����', }, { id: '', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], timeBeginEnd: '09:10 - 10:10', type: '����', idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '10:20 - 11:20', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '11:30 - 12:30', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '13:00 - 14:00', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '14:10 - 15:10', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '15:20 - 16:20', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, { id: '', timeBeginEnd: '16:30 - 17:30', type: '����', courseTimeOneList: [ { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, { maxNumber: '', price: '', teacherId: '', teacherName: '', theDate: '', theTimeBeginToEnd: '', unitPrice: '', monthDayStr: '', }, ], idspan: 1, iddis: false, courseTimeOneListspan: 1, courseTimeOneListdis: false, }, ]
核心代码
courseTimeList.filter((item) => { item.courseTimeOneList = item.courseTimeOneList.filter((o) => { //这里判断name中是否包含所搜索的内容,filter函数的返回值是布尔值,为true则表示存在会保留在数组中,为false则过滤掉 if (o.teacherName) { return o } }) if (item.courseTimeOneList.length > 0) { return item //这里一定要写返回值,不然过滤的结果会一直是空 } })
最终控制台打印的结果
[ { "id": 1, "courseTimeOneList": [ { "maxNumber": "30", "price": "", "teacherId": "5b7bbe50da9540fcb1d84e4e2f00fa8b", "teacherName": "888999", "theDate": "2021-11-19", "theTimeBeginToEnd": "", "index": [ 0, 3 ], "unitPrice": "50", "monthDayStr": "����" } ], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false, "timeBeginEnd": "08:00 - 09:00", "type": "����" }, { "id": "", "courseTimeOneList": [], "timeBeginEnd": "09:10 - 10:10", "type": "����", "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "10:20 - 11:20", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "11:30 - 12:30", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "13:00 - 14:00", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "14:10 - 15:10", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "15:20 - 16:20", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false }, { "id": "", "timeBeginEnd": "16:30 - 17:30", "type": "����", "courseTimeOneList": [], "idspan": 1, "iddis": false, "courseTimeOneListspan": 1, "courseTimeOneListdis": false } ]
以上就是多维数组的遍历方法,看懂的小伙伴记得收藏我。
小礼物走一波,支持作者
赏还没有人赞赏,支持一波吧