我使用使用Angular 5.1构建的NoSQL数据库创建一个调度应用程序
这是我的样本数据
staff = [
{
name: 'John Stewart',
updatedTimestamp: '1520221418024',
office: 'A',
scheduledDay: '03/25/2018',
scheduledHours: [114, 115, 116, 117, 118, 119, 120, 121]
},
{
name: 'John Stewart',
availableHours: [100, 101, 102, 114, 115, 116, 117, 118, 119, 120, 121, 200, 201, 202, 214, 215, 216, 217, 218, 220, 221, 222, 223]
},
{
name: 'John Stewart',
updatedTimestamp: '1520221418026',
office: 'A',
scheduledDay: '03/26/2018',
scheduledHours: [214, 215, 216, 217, 218]
},
{
name: 'John Stewart',
updatedTimestamp: '1520221418025',
office: 'B',
scheduledDay: '03/26/2018',
scheduledHours: [220, 221, 222, 223]
},
{
name: 'Hal Jordan',
availableHours: [300, 301, 302, 203, 304, 305, 306, 307, 308, 309, 310, 111, 112, 113, 114, 115, 116, 117, 118, 119, 200, 201, 202],
},
{
name: 'Hal Jordan',
updatedTimestamp: '1520221418025',
office: 'B',
scheduledDay: '03/26/2018',
scheduledHours: [218, 219, 220, 221, 222, 223]
},
{
name: 'Guy Gardner',
availableHours: [300, 301, 302, 203, 304, 305, 306, 307, 308, 309, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 200, 201, 202],
},
{
name: 'Guy Gardner',
updatedTimestamp: '1520221418025',
office: 'CB',
scheduledDay: '03/26/2018',
scheduledHours: [220, 221, 222, 223]
}
]
100是星期日午夜
101是周日1 A.M
202是周一凌晨2点。等等。
组合这些数据的最佳方式是什么,以便每个用户都设置可用的小时数,并设置预定的小时数,以便我可以比较它们,只显示与可用小时数匹配的人员,但不会安排在那段时间。
我已经尝试将Object.assign与过滤器一起使用,该过滤器可以与其他对象一起使用,但我无法使用它来处理这些数据。
combineObjects(a, b) {
const result = this.staff[0].map(val => {
return Object.assign({}, val, this.staff[1].filter(v => v.name === val.name)[0]);
});
this.allStaff.push(result);
};
我的最终目标是根据管理员选择的时间动态显示可用者,而不重复同一个人。如果John Stewart在办公室A的星期一下午2点到晚上9点工作,我不希望他在那个时间段内的任何地方都可以安排。
答案 0 :(得分:1)
你很亲密。您只需使用.reduce()
代替.map()
:
const staff = [
{
name: 'John Stewart',
updatedTimestamp: '1520221418024',
office: 'A',
scheduledDay: '03/25/2018',
scheduledHours: [114, 115, 116, 117, 118, 119, 120, 121]
},
{
name: 'John Stewart',
availableHours: [100, 101, 102, 114, 115, 116, 117, 118, 119, 120, 121, 200, 201, 202, 214, 215, 216, 217, 218, 220, 221, 222, 223]
},
{
name: 'John Stewart',
updatedTimestamp: '1520221418026',
office: 'A',
scheduledDay: '03/26/2018',
scheduledHours: [214, 215, 216, 217, 218]
},
{
name: 'John Stewart',
updatedTimestamp: '1520221418025',
office: 'B',
scheduledDay: '03/26/2018',
scheduledHours: [220, 221, 222, 223]
},
{
name: 'Hal Jordan',
availableHours: [300, 301, 302, 203, 304, 305, 306, 307, 308, 309, 310, 111, 112, 113, 114, 115, 116, 117, 118, 119, 200, 201, 202],
},
{
name: 'Hal Jordan',
updatedTimestamp: '1520221418025',
office: 'B',
scheduledDay: '03/26/2018',
scheduledHours: [218, 219, 220, 221, 222, 223]
},
{
name: 'Guy Gardner',
availableHours: [300, 301, 302, 203, 304, 305, 306, 307, 308, 309, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 200, 201, 202],
},
{
name: 'Guy Gardner',
updatedTimestamp: '1520221418025',
office: 'CB',
scheduledDay: '03/26/2018',
scheduledHours: [220, 221, 222, 223]
}
];
function combineObjects(a, b) {
return staff.reduce((currentArr, nextVal) => {
return currentArr.filter((current) => current.name === nextVal.name).length === 0
? [].concat(currentArr, nextVal)
: currentArr.map((current) => current.name === nextVal.name ? Object.assign({}, current, nextVal) : current);
}, []);
}
console.log(combineObjects('', ''));