使用 .filter JS 删除行的问题
Problem with removing rows with .filter JS
var result_database = [
{ id: 49, absentName: 'man1', absentday: 1 },
{ id: 50, absentName: 'man2', absentday: 1 },
{ id: 49, absentName: 'man1', absentday: 2 },
{ id: 50, absentName: 'man2', absentday: 2 },
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 },
{ id: 50, absentName: 'man2', absentday: 3 }
]
因此,absent_date
和 absent_days
是 return 当前日期值,星期一是 1。
const absent_date = new Date();
const absent_days = absent_date.getDay();
其余:
let absent_remove = result_database.filter(item => item.absentday != absent_days);
const absent_duplicate = [...new Map(absent_remove.map(item => [item.id, item])).values()]
console.log("Absent Result", absent_duplicate);
所以基本上,如果 absentday
匹配 absent_days
,absent_remove
会删除数据库行。然后我得到一堆重复项,我用 absent_duplicate 解决了这些重复项,删除了重复项。我在下面留下了这些结果。
Absent Result [
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 50, absentName: 'man2', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 }
]
周一(1)Man1和Man2都缺席了,但是还在query中,问题是,怎么把他们拿出来?最后的结果应该是man3一个人。
试试这个:
res = [
{ id: 49, absentName: 'man1', absentday: 1 },
{ id: 50, absentName: 'man2', absentday: 1 },
{ id: 49, absentName: 'man1', absentday: 2 },
{ id: 50, absentName: 'man2', absentday: 2 },
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 },
{ id: 50, absentName: 'man2', absentday: 3 }
]
answer = res.filter(x => !res.find(y => x.id == y.id && y.absentday == 1))
console.log(answer)
对于您结果中的每个条目,如果没有其他条目缺席日期为“1”,我们只会保留它。
var result_database = [
{ id: 49, absentName: 'man1', absentday: 1 },
{ id: 50, absentName: 'man2', absentday: 1 },
{ id: 49, absentName: 'man1', absentday: 2 },
{ id: 50, absentName: 'man2', absentday: 2 },
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 },
{ id: 50, absentName: 'man2', absentday: 3 }
]
因此,absent_date
和 absent_days
是 return 当前日期值,星期一是 1。
const absent_date = new Date();
const absent_days = absent_date.getDay();
其余:
let absent_remove = result_database.filter(item => item.absentday != absent_days);
const absent_duplicate = [...new Map(absent_remove.map(item => [item.id, item])).values()]
console.log("Absent Result", absent_duplicate);
所以基本上,如果 absentday
匹配 absent_days
,absent_remove
会删除数据库行。然后我得到一堆重复项,我用 absent_duplicate 解决了这些重复项,删除了重复项。我在下面留下了这些结果。
Absent Result [
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 50, absentName: 'man2', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 }
]
周一(1)Man1和Man2都缺席了,但是还在query中,问题是,怎么把他们拿出来?最后的结果应该是man3一个人。
试试这个:
res = [
{ id: 49, absentName: 'man1', absentday: 1 },
{ id: 50, absentName: 'man2', absentday: 1 },
{ id: 49, absentName: 'man1', absentday: 2 },
{ id: 50, absentName: 'man2', absentday: 2 },
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 },
{ id: 50, absentName: 'man2', absentday: 3 }
]
answer = res.filter(x => !res.find(y => x.id == y.id && y.absentday == 1))
console.log(answer)
对于您结果中的每个条目,如果没有其他条目缺席日期为“1”,我们只会保留它。