问题:有以下数组
var reportList = [
{ reportName: '报告一', reportType: 1, reportTime: '20190524', isRelease: true },
{ reportName: '报告二', reportType: 2, reportTime: '20190321', isRelease: true },
{ reportName: '报告三', reportType: 1, reportTime: '20180324', isRelease: true },
{ reportName: '报告四', reportType: 2, reportTime: '20190111', isRelease: false },
{ reportName: '报告五', reportType: 1, reportTime: '20180424', isRelease: false },
{ reportName: '报告六', reportType: 2, reportTime: '20190228', isRelease: true },
{ reportName: '报告七', reportType: 1, reportTime: '20180324', isRelease: true },
{ reportName: '报告八', reportType: 2, reportTime: '20171111', isRelease: true },
];
其中reportType表示报告类型,1为检查报告,2为检验报告;isRelease:表示改报告是否已发布;处理该数据得到四个数组:
1. 已发布检验报告
2. 已发布检查报告
3. 未发布检验报告
4. 未发布检查报告
并且将时间处理成2019-06-25的格式
解答:
var reportList = [
{ reportName: '报告一', reportType: 1, reportTime: '20190524', isRelease: true },
{ reportName: '报告二', reportType: 2, reportTime: '20190321', isRelease: true },
{ reportName: '报告三', reportType: 1, reportTime: '20180324', isRelease: true },
{ reportName: '报告四', reportType: 2, reportTime: '20190111', isRelease: false },
{ reportName: '报告五', reportType: 1, reportTime: '20180424', isRelease: false },
{ reportName: '报告六', reportType: 2, reportTime: '20190228', isRelease: true },
{ reportName: '报告七', reportType: 1, reportTime: '20180324', isRelease: true },
{ reportName: '报告八', reportType: 2, reportTime: '20171111', isRelease: true },
];
const arr1 = [];
const arr2 = [];
const arr3 = [];
const arr4 = [];
reportList.forEach((item) => {
const temp = item.reportTime;
item.reportTime = temp.substr(0,4) + '-' + temp.substr(4,2) + '-' + temp.substr(6,2)
if (item.reportType == 1) {
if (item.isRelease) {
arr1.push(item);
} else {
arr2.push(item);
}
} else {
if (item.isRelease) {
arr3.push(item);
} else {
arr4.push(item);
}
}
})
console.log(arr1);
console.log(arr2);
console.log(arr3);
console.log(arr4);