Arrow Functions and Destructuring for Object Transformation
Arrow functions and destructuring are two powerful features of JavaScript. When used in combination, they present a cool pattern that I like to call object transformation. The point is to write a simple function to change the structure of a given object while keeping (some of) the attribute values.
Simple Transformers
First, let’s write a trivial transformer function that looks very symmetrical, like this:
let extract = ({ fruit }) => ({ fruit })
At first glance, it may seem like this function does nothing. Let’s run it against an object:
>> extract({ fruit: "apple" })
<- Object { fruit: "apple" }
It returns the same object that it received. Let’s now feed it with a more complex object:
>> extract({ fruit: "apple", "color": "red" })
<- Object { fruit: "apple" }
Now we see that this transformer function returns a new object with only the fruit attribute present.
So how does it work? This extract is a lambda function that accepts an object with a fruit property (left part of the expression). It returns an object with the same fruit property (right part of the expression) and the same value, disregarding all other properties. The => symbol separates the arguments from the lambda function body.
The object to the right of the arrow should be placed in parentheses because otherwise, the JavaScript interpreter parses the curly braces as a function body, not an object:
>> let wrongExtract = ({ fruit }) => { fruit }
<- undefined
>> wrongExtract({fruit: "apple"})
<- undefined
The body of the wrongExtract function contains just a single expression — the fruit variable. This is a valid JavaScript expression, but it doesn’t return any value from the function, so the function returns undefined.
Now let’s use destructuring to extract some deeply nested data. We want to get a fruit from a box that’s itself located inside a container:
>> let extractFruit = ({ container: { box: { fruit } } })
=> ({ fruit })
>> extractFruit({ container: { box: { fruit: "apple" } } })
<- Object { fruit: "apple" }
We’ve just written a function to extract a deeply nested structure from the object, using destructuring.
Complex Cases
But destructuring is useful not only for extracting the data. Let’s use it to exchange values of two deeply nested attributes of an object. The object is a bit more complicated here, so we won’t try to write the function as a one-liner — formatting it across multiple lines should improve readability:
>> let makeItRight =
({ starwars:
{
han: { shotfirst: hanShotFirst },
greedo: { shotfirst: greedoShotFirst }
}
}) =>
({ starwars:
{
han: { shotfirst: greedoShotFirst },
greedo: { shotfirst: hanShotFirst }
}
})
Still, there’s a lot to digest here. We have a complex object with two (boolean) attributes on different levels of nesting. We want to exchange the values of those attributes, so we destructure them to the hanShotFirst and greedoShotFirst variables.
Using these variables, we return a new object with the same structure, but with these attribute values exchanged.
>> makeItRight({
starwars: {
han: { shotfirst: false },
greedo: { shotfirst: true }
}
})
<- {
starwars: {
han: { shotfirst: true },
greedo: { shotfirst: false }
}
}
Finally, let’s use array destructuring to make an (opinionated) extraction of an element from an array:
>> let selectBestMovie = ({ starwars: [ first, second, third, ...rest ]})
=> ({ best: second })
>> selectBestMovie({
starwars: [
'A New Hope',
'The Empire Strikes Back',
'Return of the Jedi'
]
})
<- {best: "The Empire Strikes Back"}
I think the way you can use arrow functions and destructuring assignment for object transformation is pretty cool. There could be of course many more use cases for destructuring, but this is my favorite use case so far.