We don't always control the data we need in our applications, and that means we often find ourselves massaging and transforming our data. In this lesson, we'll learn how to transform objects in a declarative way using ramda's evolve function.
Assume we have this object:
const product = { name: 'cog', price: 100, details: { shippingInfo: { weight: 7, method: 'ups' } } }
Let's say we have to do
- Uppcase for name
- Double price
- Change weight to 8
Using Ramda's evolve:
const product = { name: 'cog', price: 100, details: { shippingInfo: { weight: 7, method: 'ups' } } } const transformation = { name: R.toUpper, price: R.multiply(2), details: { shippingInfo: { weight: R.inc } } }; const adjustProduct = R.evolve(transformation); const result = adjustProduct(product)