Plucking properties from object arrays

One of the more common uses for is extracting properties from objects. Instead of using individual arrow functions, we can create a reusable function that does the plucking for us.

const countries = [
  { name: 'France', capital: 'Paris'  },
  { name: 'Spain',  capital: 'Madrid' },
  { name: 'Italy',  capital: 'Rome'   }

// we can extract the attributes with individual arrow functions =>     // ⇒ ['France', 'Spain', 'Italy'] =>  // ⇒ ['Paris', 'Madrid', 'Rome']

// this function allows us to write that arrow function shorter
const pluck = property => element => element[property]'name'))     // ⇒ ['France', 'Spain', 'Italy']'capital'))  // ⇒ ['Paris', 'Madrid', 'Rome']

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.