discardWhen
discardWhen
(predicate: Predicate) => (collection: Collection) => Collection
Discard entries that pass a condition
const gt2 = n => n > 2 const discardWhenGt2 = discardWhen(gt2) const resultArr = discardWhenGt2([4, 1, 3, 2]) console.log(resultArr) // is [1, 2] const resultObj = discardWhenGt2({ d: 4, a: 1, c: 3, b: 2, }) console.log(resultObj) // is { a: 1, b: 2 }
const gt2 = (n: number) => n > 2 const discardWhenGt2 = discardWhen(gt2) const resultArr = discardWhenGt2([4, 1, 3, 2]) console.log(resultArr) // is [1, 2] const resultObj = discardWhenGt2({ d: 4, a: 1, c: 3, b: 2, }) console.log(resultObj) // is { a: 1, b: 2 }
const gt2 = n => n > 2 const discardWhenGt2 = discardWhen(gt2) const resultArr = discardWhenGt2([4, 1, 3, 2]) console.log(resultArr) // is [1, 2] const resultObj = discardWhenGt2({ d: 4, a: 1, c: 3, b: 2, }) console.log(resultObj) // is { a: 1, b: 2 }
const gt2 = (n: number) => n > 2 const discardWhenGt2 = discardWhen(gt2) const resultArr = discardWhenGt2([4, 1, 3, 2]) console.log(resultArr) // is [1, 2] const resultObj = discardWhenGt2({ d: 4, a: 1, c: 3, b: 2, }) console.log(resultObj) // is { a: 1, b: 2 }
Sometimes we want to discard data. Below, we have an array of soda can weight measurements fresh off the assembly. We can't ship any outside our threshold of +/-0.02 fl oz from 12. Let's discard them.
const sodaWeights = [12, 11.99, 12.2, 11.88, 12.02] const isOutsideThreshold = flOz => flOz > 12.02 || flOz < 11.98 const discardBadSodas = discardWhen(isOutsideThreshold) const updatedSodas = discardBadSodas(sodaWeights) console.log(updatedSodas) // is [12, 11.99, 12.02]
const sodaWeights = [12, 11.99, 12.2, 11.88, 12.02] const isOutsideThreshold = (flOz: number) => flOz > 12.02 || flOz < 11.98 const discardBadSodas = discardWhen(isOutsideThreshold)<number[]> const updatedSodas = discardBadSodas(sodaWeights) console.log(updatedSodas) // is [12, 11.99, 12.02]
const sodaWeights = [ 12, 11.99, 12.2, 11.88, 12.02, ] const isOutsideThreshold = flOz => flOz > 12.02 || flOz < 11.98 const discardBadSodas = discardWhen( isOutsideThreshold ) const updatedSodas = discardBadSodas(sodaWeights) console.log(updatedSodas) // is [12, 11.99, 12.02]
const sodaWeights = [ 12, 11.99, 12.2, 11.88, 12.02, ] const isOutsideThreshold = (flOz: number) => flOz > 12.02 || flOz < 11.98 const discardBadSodas = discardWhen( isOutsideThreshold )<number[]> const updatedSodas = discardBadSodas(sodaWeights) console.log(updatedSodas) // is [12, 11.99, 12.02]