zoukankan      html  css  js  c++  java
  • [Ramda] Declaratively Map Data Transformations to Object Properties Using Ramda evolve

    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)
  • 相关阅读:
    万能清除
    CSS追加笔记
    函数追加笔记
    jQuery实现放大镜效果
    jQuery获取元素的方法
    JavaScript
    jQuery三级联动
    DOM追加笔记
    数据结构与算法之美01
    RPC架构简介与原理
  • 原文地址:https://www.cnblogs.com/Answer1215/p/6307207.html
Copyright © 2011-2022 走看看