mPick
mPick
(keys: EntryKey[]) => (collection: EntryCollection) => collection
Pick entries from a collection by key
Consider pick instead.
This is for the uncommon case where mutatingcollection
is required.const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = mPick(['a', 'c']) const mutatedObj = pickAC(obj) console.log(mutatedObj === obj) // is true console.log(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = mPick(['a', 'c']) const mutatedObj = pickAC(obj) console.log(mutatedObj === obj) // is true console.log(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = mPick(['a', 'c']) const mutatedObj = pickAC(obj) console.log(mutatedObj === obj) // is true console.log(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = mPick(['a', 'c']) const mutatedObj = pickAC(obj) console.log(mutatedObj === obj) // is true console.log(obj) // is { a: 1, c: 3 }
Sometimes we want to pick entries by key. Below, we run a cat shelter. A visitor, Grace, is adopting Bella and Leo. Let's get the cats' information to send Grace off with a copy.
const cats = { bella: { age: 3, notes: 'likes her alone time', breed: 'domestic shorthair', }, leo: { age: 2, notes: 'enjoys food too much', breed: 'domestic longhair', }, molly: { age: 4, notes: 'energetic in the morning', breed: 'domestic shorthair', }, } const catsToAdopt = ['bella', 'leo'] const adoptBellaAndLeo = mPick(catsToAdopt) adoptBellaAndLeo(cats) console.log(cats) // is { // bella: { // age: 3 // notes: likes her alone time // breed: domestic shorthair // } // leo: { // age: 2 // notes: enjoys food too much // breed: domestic longhair // } // }
type Cat = { age: number notes: string breed: string } type Cats = Record<string, Cat> const cats: Cats = { bella: { age: 3, notes: 'likes her alone time', breed: 'domestic shorthair', }, leo: { age: 2, notes: 'enjoys food too much', breed: 'domestic longhair', }, molly: { age: 4, notes: 'energetic in the morning', breed: 'domestic shorthair', }, } const catsToAdopt = ['bella', 'leo'] const adoptBellaAndLeo = mPick(catsToAdopt)<Cats> adoptBellaAndLeo(cats) console.log(cats) // is { // bella: { // age: 3 // notes: likes her alone time // breed: domestic shorthair // } // leo: { // age: 2 // notes: enjoys food too much // breed: domestic longhair // } // }
const cats = { bella: { age: 3, notes: 'likes her alone time', breed: 'domestic shorthair', }, leo: { age: 2, notes: 'enjoys food too much', breed: 'domestic longhair', }, molly: { age: 4, notes: 'energetic in the morning', breed: 'domestic shorthair', }, } const catsToAdopt = ['bella', 'leo'] const adoptBellaAndLeo = mPick(catsToAdopt) adoptBellaAndLeo(cats) console.log(cats) // is { // bella: { // age: 3 // notes: likes her alone time // breed: domestic shorthair // } // leo: { // age: 2 // notes: enjoys food too much // breed: domestic longhair // } // }
type Cat = { age: number notes: string breed: string } type Cats = Record<string, Cat> const cats: Cats = { bella: { age: 3, notes: 'likes her alone time', breed: 'domestic shorthair', }, leo: { age: 2, notes: 'enjoys food too much', breed: 'domestic longhair', }, molly: { age: 4, notes: 'energetic in the morning', breed: 'domestic shorthair', }, } const catsToAdopt = ['bella', 'leo'] const adoptBellaAndLeo = mPick(catsToAdopt)<Cats> adoptBellaAndLeo(cats) console.log(cats) // is { // bella: { // age: 3 // notes: likes her alone time // breed: domestic shorthair // } // leo: { // age: 2 // notes: enjoys food too much // breed: domestic longhair // } // }