Picking object properties

With Array.prototype.reduce(), we can extract only the properties of an object we want to pass along.

// we don’t want to pass all of this information along all the time
const user = {
  email: 'ckent@dailyplanet.com',
  password: 'i-am-superman',
  username: 'clarkkent'

// this function receives an object and a list of properties to extract
const pick = (object, props) => props.reduce((picked, prop) => ({
  [prop]: object[prop]
}), {})

// we can use `pick` to get only the information we need
const condensedUser = pick(user, ['username', 'email'])
// ⇒ {
//     username: 'clarkkent',
//     email: 'ckent@dailyplanet.com'
//   }

There is more to learn

Get free previews of my upcoming course materials and other bonus content to help you work smarter. I share tips straight to your inbox once a week. You can read previous mails in the newsletter archive.

Cannot show form because cookie consent was not given.