pick
pick
(keys: EntryKey[]) => (collection: EntryCollection) => EntryCollection
Pick entries from a collection by key
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = pick(['a', 'c']) pickAC(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = pick(['a', 'c']) pickAC(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = pick(['a', 'c']) pickAC(obj) // is { a: 1, c: 3 }
const obj = { a: 1, b: 2, c: 3, d: 4 } const pickAC = pick(['a', 'c']) pickAC(obj) // is { a: 1, c: 3 }
Sometimes we want to pick entries by key. Below, we run a restaurant and are picking our featured menu for the week. Let's choose chicken wings and the BLT wrap.
const fullMenu = { 'chicken wings': { price: 16, description: 'One pound tossed in sauce', }, 'smash burger': { price: 15, description: 'A double patty with classic toppings', }, 'chicken sandwich': { price: 15, description: 'Blacked, fresh toppings, on ciabatta', }, 'blt wrap': { price: 14, description: 'The classic with bacon jam and lemon aioli', }, } const pickFeaturedItems = pick(['chicken wings', 'blt wrap']) const featuredMenu = pickFeaturedItems(fullMenu) console.log(featuredMenu) // prints // { // chicken wings: { // price: 16 // description: One pound tossed in sauce // }, // blt wrap: { // price: 14 // description: The classic with bacon jam and lemon aioli // }, // }
type Item = { price: number description: string } type Menu = Record<string, Item> const fullMenu: Menu = { 'chicken wings': { price: 16, description: 'One pound tossed in sauce', }, 'smash burger': { price: 15, description: 'A double patty with classic toppings', }, 'chicken sandwich': { price: 15, description: 'Blacked, fresh toppings, on ciabatta', }, 'blt wrap': { price: 14, description: 'The classic with bacon jam and lemon aioli', }, } const pickFeaturedItems = pick(['chicken wings', 'blt wrap'])<Menu> const featuredMenu = pickFeaturedItems(fullMenu) console.log(featuredMenu) // prints // { // chicken wings: { // price: 16 // description: One pound tossed in sauce // }, // blt wrap: { // price: 14 // description: The classic with bacon jam and lemon aioli // }, // }
const fullMenu = { 'chicken wings': { price: 16, description: 'One pound tossed in sauce', }, 'smash burger': { price: 15, description: 'A double patty with classic toppings', }, 'chicken sandwich': { price: 15, description: 'Blacked, fresh toppings, on ciabatta', }, 'blt wrap': { price: 14, description: 'The classic with bacon jam and lemon aioli', }, } const pickFeaturedItems = pick([ 'chicken wings', 'blt wrap', ]) const featuredMenu = pickFeaturedItems(fullMenu) console.log(featuredMenu) // prints // { // chicken wings: { // price: 16 // description: One pound tossed in sauce // }, // blt wrap: { // price: 14 // description: The classic with bacon jam and lemon aioli // }, // }
type Item = { price: number description: string } type Menu = Record<string, Item> const fullMenu: Menu = { 'chicken wings': { price: 16, description: 'One pound tossed in sauce', }, 'smash burger': { price: 15, description: 'A double patty with classic toppings', }, 'chicken sandwich': { price: 15, description: 'Blacked, fresh toppings, on ciabatta', }, 'blt wrap': { price: 14, description: 'The classic with bacon jam and lemon aioli', }, } const pickFeaturedItems = pick([ 'chicken wings', 'blt wrap', ])<Menu> const featuredMenu = pickFeaturedItems(fullMenu) console.log(featuredMenu) // prints // { // chicken wings: { // price: 16 // description: One pound tossed in sauce // }, // blt wrap: { // price: 14 // description: The classic with bacon jam and lemon aioli // }, // }