我有一个对象数组,每个对象都有reportYear
和reportWeek
属性。目前,这些对象仅涵盖从开始日期到结束日期的几周和几年。如何插入更多的对象来填充缺失周的数组?
例如:
输入:
[
{"reportYear":2017,"reportWeek":49,"employeeCode":100,"employeeName":"Peter","importanceDegree":182.0},
{"reportYear":2017,"reportWeek":52,"employeeCode":100,"employeeName":"Peter","importanceDegree":77.0},
{"reportYear":2018,"reportWeek":2,"employeeCode":100,"employeeName":"Peter","importanceDegree":101.0},
]
输出:
[
{"reportYear":2017,"reportWeek":49,"employeeCode":100,"employeeName":"Peter","importanceDegree":182.0},
{"reportYear":2017,"reportWeek":50,"employeeCode":100,"employeeName":"Peter","importanceDegree":0},
{"reportYear":2017,"reportWeek":51,"employeeCode":100,"employeeName":"Peter","importanceDegree":0},
{"reportYear":2017,"reportWeek":52,"employeeCode":100,"employeeName":"Peter","importanceDegree":77.0},
{"reportYear":2018,"reportWeek":1,"employeeCode":100,"employeeName":"Peter","importanceDegree":0},
{"reportYear":2018,"reportWeek":2,"employeeCode":100,"employeeName":"Peter","importanceDegree":101.0},
]
答案 0 :(得分:1)
您需要从开始reportYear
和reportWeek
开始循环,搜索匹配的项目,如果项目不存在则添加项目,然后递增一周(如果需要,还有一年)直到你到达最后一周和一年:
const data = [
{"reportYear":2017,"reportWeek":49,"employeeCode":100,"employeeName":"Peter","importanceDegree":182.0},
{"reportYear":2017,"reportWeek":51,"employeeCode":100,"employeeName":"Peter","importanceDegree":231.0},
{"reportYear":2017,"reportWeek":52,"employeeCode":100,"employeeName":"Peter","importanceDegree":77.0},
{"reportYear":2018,"reportWeek":1,"employeeCode":100,"employeeName":"Peter","importanceDegree":55.0},
{"reportYear":2018,"reportWeek":2,"employeeCode":100,"employeeName":"Peter","importanceDegree":101.0},
{"reportYear":2018,"reportWeek":3,"employeeCode":100,"employeeName":"Peter","importanceDegree":982.0},
{"reportYear":2018,"reportWeek":4,"employeeCode":100,"employeeName":"Peter","importanceDegree":744.0},
{"reportYear":2018,"reportWeek":8,"employeeCode":100,"employeeName":"Peter","importanceDegree":698.0},
{"reportYear":2018,"reportWeek":9,"employeeCode":100,"employeeName":"Peter","importanceDegree":235.0},
{"reportYear":2018,"reportWeek":12,"employeeCode":100,"employeeName":"Peter","importanceDegree":657.0},
{"reportYear":2018,"reportWeek":13,"employeeCode":100,"employeeName":"Peter","importanceDegree":251.0},
{"reportYear":2018,"reportWeek":14,"employeeCode":100,"employeeName":"Peter","importanceDegree":147.0},
{"reportYear":2018,"reportWeek":15,"employeeCode":100,"employeeName":"Peter","importanceDegree":156.0},
{"reportYear":2018,"reportWeek":19,"employeeCode":100,"employeeName":"Peter","importanceDegree":481.0},
{"reportYear":2018,"reportWeek":20,"employeeCode":100,"employeeName":"Peter","importanceDegree":639.0},
{"reportYear":2018,"reportWeek":21,"employeeCode":100,"employeeName":"Peter","importanceDegree":256.0},
{"reportYear":2018,"reportWeek":30,"employeeCode":100,"employeeName":"Peter","importanceDegree":324.0},
{"reportYear":2018,"reportWeek":31,"employeeCode":100,"employeeName":"Peter","importanceDegree":852.0},
{"reportYear":2019,"reportWeek":3,"employeeCode":100,"employeeName":"Peter","importanceDegree":361.0},
];
let year = 2017;
let week = 49;
while (year !== 2019 || week !== 3) {
const foundObjectIndex = data.findIndex(
({ reportYear, reportWeek }) => reportYear === year && reportWeek === week
);
if (foundObjectIndex === -1) {
data.push({
reportYear: year,
reportWeek: week,
employeeCode: 100,
employeeName: 'Peter',
importanceDegree: 0,
});
}
week++;
if (week === 53) {
week = 1;
year++;
}
}
data.sort((a, b) => {
if (a.reportYear !== b.reportYear) return a.reportYear - b.reportYear;
if (a.reportWeek != b.reportWeek) return a.reportWeek - b.reportWeek;
return b.importanceDegree - a.importanceDegree;
});
console.log(data);
ES5版本:
var year = 2017;
var week = 49;
while (year !== 2019 || week !== 3) {
var foundObjectIndex = data.findIndex(function (_ref) {
var reportYear = _ref.reportYear,
reportWeek = _ref.reportWeek;
return reportYear === year && reportWeek === week;
});
if (foundObjectIndex === -1) {
data.push({
reportYear: year,
reportWeek: week,
employeeCode: 100,
employeeName: 'Peter',
importanceDegree: 0
});
}
week++;
if (week === 53) {
week = 1;
year++;
}
}
data.sort(function (a, b) {
if (a.reportYear !== b.reportYear) return a.reportYear - b.reportYear;
if (a.reportWeek != b.reportWeek) return a.reportWeek - b.reportWeek;
return b.importanceDegree - a.importanceDegree;
});
console.log(data);