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將回傳當前日期值,星期一為 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);
所以基本上,如果匹配,absent_remove則洗掉資料庫行。然后我得到一堆重復項,我用absent_duplicate 解決了這些重復項,洗掉了重復項。我在下面留下了這些結果。absentdayabsent_days
Absent Result [
{ id: 49, absentName: 'man1', absentday: 3 },
{ id: 50, absentName: 'man2', absentday: 3 },
{ id: 51, absentName: 'man3', absentday: 4 }
]
Man1 和 Man2 星期一(1)都缺席了,但他們還在查詢中,問題是,我如何把他們拿出來?最終結果應該是 man3 單獨的。
uj5u.com熱心網友回復:
試試這個:
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”的其他條目時才保留它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/471268.html
標籤:javascript mysql
下一篇:如何洗掉檔案中的重復行?
