mDiscardLastWhile
mDiscardLastWhile
(predicate: Predicate) => (anArray: array) => anArray
Discard the last elements that pass a condition
Consider discardLastWhile instead.
This is for the uncommon case where mutatinganArray
is required.const gt2 = n => n > 2 const mDiscardLastWhileGt2 = mDiscardLastWhile(gt2) const arr = [1, 2, 3, 4] const mutatedArr = mDiscardLastWhileGt2(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2]
const gt2 = (n: number) => n > 2 const mDiscardLastWhileGt2 = mDiscardLastWhile(gt2) const arr = [1, 2, 3, 4] const mutatedArr = mDiscardLastWhileGt2(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2]
const gt2 = n => n > 2 const mDiscardLastWhileGt2 = mDiscardLastWhile(gt2) const arr = [1, 2, 3, 4] const mutatedArr = mDiscardLastWhileGt2(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2]
const gt2 = (n: number) => n > 2 const mDiscardLastWhileGt2 = mDiscardLastWhile(gt2) const arr = [1, 2, 3, 4] const mutatedArr = mDiscardLastWhileGt2(arr) console.log(mutatedArr === arr) // is true console.log(arr) // is [1, 2]
This is useful when we have sorted data and don't need to filter over all of it. Below, we have a list of accounts ordered by activity. Let's remove accounts that have been inactive for at least a year.
const accounts = [ { id: 'ken@example.com', lastActive: '2024 2 16' }, { id: 'chris@example.com', lastActive: '2024 2 15' }, { id: 'liz@example.com', lastActive: '2024 2 15' }, ] const yearAgo = new Date('2024 2 15') const isInactiveForAYear = ({ lastActive }) => { const lastActiveDate = new Date(lastActive) return lastActiveDate <= yearAgo } const discardInactiveAccounts = mDiscardLastWhile(isInactiveForAYear) discardInactiveAccounts(accounts) console.log(accounts) // is [{ // id: ken@example.com // lastActive: 2024 2 16 // }]
type Account = { id: string lastActive: string } const accounts: Account[] = [ { id: 'ken@example.com', lastActive: '2024 2 16' }, { id: 'chris@example.com', lastActive: '2024 2 15' }, { id: 'liz@example.com', lastActive: '2024 2 15' }, ] const yearAgo = new Date('2024 2 15') const beenInactiveForAYear = ({ lastActive }: Account) => { const lastActiveDate = new Date(lastActive) return lastActiveDate <= yearAgo } const discardInactiveAccounts = mDiscardLastWhile(beenInactiveForAYear) discardInactiveAccounts(accounts) console.log(accounts) // is [{ // id: ken@example.com // lastActive: 2024 2 16 // }]
const accounts = [ { id: 'ken@example.com', lastActive: '2024 2 16', }, { id: 'chris@example.com', lastActive: '2024 2 15', }, { id: 'liz@example.com', lastActive: '2024 2 15', }, ] const yearAgo = new Date('2024 2 15') const isInactiveForAYear = ({ lastActive }) => { const lastActiveDate = new Date(lastActive) return lastActiveDate <= yearAgo } const discardInactiveAccounts = mDiscardLastWhile( isInactiveForAYear ) discardInactiveAccounts(accounts) console.log(accounts) // is [{ // id: ken@example.com // lastActive: 2024 2 16 // }]
type Account = { id: string lastActive: string } const accounts: Account[] = [ { id: 'ken@example.com', lastActive: '2024 2 16', }, { id: 'chris@example.com', lastActive: '2024 2 15', }, { id: 'liz@example.com', lastActive: '2024 2 15', }, ] const yearAgo = new Date('2024 2 15') const beenInactiveForAYear = ({ lastActive, }: Account) => { const lastActiveDate = new Date(lastActive) return lastActiveDate <= yearAgo } const discardInactiveAccounts = mDiscardLastWhile( beenInactiveForAYear ) discardInactiveAccounts(accounts) console.log(accounts) // is [{ // id: ken@example.com // lastActive: 2024 2 16 // }]