Merging many objects
We can shallowly merge objects without having to type out all properties by hand.
Read full articleOne of the more common tasks when mapping over arrays in JavaScript is extracting properties from objects. Instead of using individual arrow functions, we can create a reusable helper 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
countries.map(country => country.name) // ⇒ ["France", "Spain", "Italy"]
countries.map(country => country.capital) // ⇒ ["Paris", "Madrid", "Rome"]
// this function allows us to write that arrow function shorter
const pluck = property => element => element[property]
countries.map(pluck("name")) // ⇒ ["France", "Spain", "Italy"]
countries.map(pluck("capital")) // ⇒ ["Paris", "Madrid", "Rome"]
const countries = [
{ name: "France", capital: "Paris" },
{ name: "Spain", capital: "Madrid" },
{ name: "Italy", capital: "Rome" }
]
// we can extract the attributes with individual arrow functions
countries.map(country => country.name) // ⇒ ["France", "Spain", "Italy"]
countries.map(country => country.capital) // ⇒ ["Paris", "Madrid", "Rome"]
// this function allows us to write that arrow function shorter
const pluck = property => element => element[property]
countries.map(pluck("name")) // ⇒ ["France", "Spain", "Italy"]
countries.map(pluck("capital")) // ⇒ ["Paris", "Madrid", "Rome"]
const countries = [
{ name: "France", capital: "Paris" },
{ name: "Spain", capital: "Madrid" },
{ name: "Italy", capital: "Rome" }
]
// we can extract the attributes with individual arrow functions
countries.map(country => country.name) // ⇒ ["France", "Spain", "Italy"]
countries.map(country => country.capital) // ⇒ ["Paris", "Madrid", "Rome"]
// this function allows us to write that arrow function shorter
const pluck = property => element => element[property]
countries.map(pluck("name")) // ⇒ ["France", "Spain", "Italy"]
countries.map(pluck("capital")) // ⇒ ["Paris", "Madrid", "Rome"]
const countries = [
{ name: "France", capital: "Paris" },
{ name: "Spain", capital: "Madrid" },
{ name: "Italy", capital: "Rome" }
]
// we can extract the attributes with individual arrow functions
countries.map(country => country.name) // ⇒ ["France", "Spain", "Italy"]
countries.map(country => country.capital) // ⇒ ["Paris", "Madrid", "Rome"]
// this function allows us to write that arrow function shorter
const pluck = property => element => element[property]
countries.map(pluck("name")) // ⇒ ["France", "Spain", "Italy"]
countries.map(pluck("capital")) // ⇒ ["Paris", "Madrid", "Rome"]
We can shallowly merge objects without having to type out all properties by hand.
Read full articleLookup objects can replace long if-else and switch statements. Checking if a key exists before accessing it makes them more resilient.
Read full articleThis helper function lets us remove all instances of specific values from an array.
Read full articleThere is now a more convenient way than reduce or loops to check if all elements in an array match a condition.
Read full articleWe can reduce an array of numbers into a single value that represents the sum of its values, without needing a traditional for-loop.
Read full articleWhen is June the fifth month of the year? When JavaScript is involved, of course. In JavaScript, January is the zero-th month.
Read full article