首页 > 技术文章 > [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve

Answer1215 2017-01-19 16:36 原文

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)

 

推荐阅读