我想簡化我的代碼并擁有一種陣列過濾方法,然后根據相應的條件分配常量警報,而不是 5 種陣列過濾方法。也許 if 陳述句或類似的東西可以解決問題?
const pendingAlerts = array.filter((a) => a.approval_status === approvalStatuses.pending && !a.canceled_at).sort(sortCommunicationsByDateRule);
const deniedAlerts = array.filter((a) => a.approval_status === approvalStatuses.denied && !a.canceled_at).sort(sortCommunicationsByDateRule);
const upcomingAlerts = array.filter((a) => isApproved(a) && !a.canceled_at && a.begin_at > today).sort(sortCommunicationsByDateRule);
const activeAlerts = array.filter((a) => isApproved(a) && !a.canceled_at && a.begin_at <= today && a.end_at > today).sort(sortCommunicationsByDateRule);
const expiredAlerts = array.filter((a) => (a.canceled_at || a.end_at < today)).sort(sortCommunicationsByDateRule);
<div className="comm-communication-list comm-alert-list wrapper">
{this.renderNotificationUI()}
{this.renderDefinitionList(pendingAlerts)}
{this.renderDefinitionList(upcomingAlerts)}
{this.renderDefinitionList(activeAlerts)}
{this.renderDefinitionList(deniedAlerts)}
{this.renderDefinitionList(expiredAlerts)}
</div>
//The ReactJS list above is rendering the Alert variables i.e.(pending, upcoming, active, denied, and expired) based upon the robust multiple filter methods
uj5u.com熱心網友回復:
您可以通過輸入陣列一次完成此操作,測驗包含在輸出陣列之一中的每個條件。您還可以使用資料將標準與輸出陣列相關聯...
const pendingAlerts = [];
const deniedAlerts = [];
const upcomingAlerts = [];
// and so on...
const criteria = [
{
array: pendingAlerts,
criterion: a => a.approval_status === approvalStatuses.pending && !a.canceled_at
},
{
array: deniedAlerts,
criterion: a => a.approval_status === approvalStatuses.denied && !a.canceled_at
},
{
array: upcomingAlerts,
criterion: a => isApproved(a) && !a.canceled_at && a.begin_at > today
},
// and so on
];
// this one loop pushes a into each of the output arrays where it matches the criterion
array.forEach(a => {
criteria.forEach(c => if (c.criterion(a)) c.array.push(a));
});
// then sort the output arrays...
criteriaForEach(c => {
c.array.sort(sortCommunicationsByDateRule)
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/370414.html
標籤:javascript 数组 if 语句 条件语句 变量赋值
上一篇:x軸標簽在svg圖中被覆寫
