Logo

dev-resources.site

for different kinds of informations.

Ramda is your friend

Published at
10/8/2021
Categories
ramda
javascript
data
Author
manoryanir
Categories
3 categories in total
ramda
open
javascript
open
data
open
Author
10 person written this
manoryanir
open
Ramda is your friend

Alt Text

Many times, When you are working with data via RestAPi, other data models.
You will find yourself need to make some manipulation for many reasons.
I think that Ramda can be a good tool for you to solve this issue.
Ramda offers a different style of coding, a style of functional programming.
Ramda makes it simple for you to build complex logic through functional composition.
You can break your logic into a small pure function.
Function input is never changed, so you can memorize it forgive a better performance if you need it.
It creates an abstraction of each part that makes it simple to solve and test.

for this example, I generate data via "json-generator.com"

const data = [
  {
    "_id": "61600fb5f380c60dd8a9b18a",
    "index": 0,
    "guid": "4c874a3b-5ae1-422b-87df-43247d150436",
    "isActive": true,
    "balance": "$1,546.67",
    "picture": "http://placehold.it/32x32",
    "age": 29,
    "eyeColor": "green",
    "name": "Flossie Bender",
    "gender": "female",
    "company": "ZENTIA",
    "email": "[email protected]",
    "phone": "+1 (831) 577-3188",
    "address": "406 Gilmore Court, Osage, American Samoa, 6924",
    "registered": "2020-04-21T08:36:18 -03:00",
    "latitude": -74.741807,
    "longitude": -54.937456,
    "languages": {
      "english": true,
      "spanish": false,
      "german": true
    },
    "tags": [
      "duis",
      "aliquip",
      "cillum",
      "duis",
      "cillum",
      "ad",
      "dolore"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Underwood Ross"
      },
      {
        "id": 1,
        "name": "Brady Mcdaniel"
      },
      {
        "id": 2,
        "name": "Mays Mccoy"
      }
    ],
    "greeting": "Hello, Flossie Bender! You have 1 unread messages.",
    "favoriteFruit": "apple"
  },
  {
    "_id": "61600fb5d3858d81df8d8b1f",
    "index": 1,
    "guid": "c576d6d6-62e0-4e68-86ef-851cedbcccdd",
    "isActive": false,
    "balance": "$2,146.29",
    "picture": "http://placehold.it/32x32",
    "age": 37,
    "eyeColor": "brown",
    "name": "Frazier Chaney",
    "gender": "male",
    "company": "APPLIDEC",
    "email": "[email protected]",
    "phone": "+1 (996) 462-2960",
    "address": "457 Clinton Street, Wescosville, Delaware, 8133",
    "registered": "2019-06-03T10:35:59 -03:00",
    "latitude": -39.993771,
    "longitude": -174.168324,
    "languages": {
      "english": true,
      "spanish": true,
      "german": false
    },
    "tags": [
      "reprehenderit",
      "veniam",
      "nostrud",
      "sunt",
      "veniam",
      "fugiat",
      "adipisicing"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Nichols Ferrell"
      },
      {
        "id": 1,
        "name": "Ingrid Houston"
      },
      {
        "id": 2,
        "name": "Wilda Branch"
      }
    ],
    "greeting": "Hello, Frazier Chaney! You have 1 unread messages.",
    "favoriteFruit": "banana"
  }
]
Enter fullscreen mode Exit fullscreen mode
const getCompanyAndAddress = R.props(['company','address']);
const haveGermanLanguage = R.path(['languages','german']);
const getSubsetNameAge = R.pick(['name','age']);

R.map(getCompanyAndAddress,data) // [["ZENTIA", "406 Gilmore Court, Osage, American Samoa, 6924"], ["APPLIDEC", "457 Clinton Street, Wescosville, Delaware, 8133"]]

R.map(haveGermanLanguage, data) // [true, false]

R.map(getSubsetNameAge)(data) // [{"age": 29, "name": "Flossie Bender"}, {"age": 37, "name": "Frazier Chaney"}]

R.map(
  R.pipe(
   R.prop("friends"), R.pluck('name')
  )
 )(data)
// [["Underwood Ross", "Brady Mcdaniel", "Mays Mccoy"], ["Nichols Ferrell", "Ingrid Houston", "Wilda Branch"]]
Enter fullscreen mode Exit fullscreen mode

Link to Ramda - https://ramdajs.com/
Enjoy 😊

ramda Article's
30 articles in total
Favicon
Lenses Pattern in JavaScript
Favicon
Farewell, Ramda
Favicon
The Functional Programming jungle: Ramda versus monads and monoids
Favicon
Performance of the spread operator
Favicon
A couple of words about Ramda for React and Redux
Favicon
Ramda & Functional Programming with React & TypeScript
Favicon
Filtering an array against another array conditionally
Favicon
寫給想跳坑的 JS 新手(Part II): coding style
Favicon
寫給想跳坑的 JS 新手(Part I): map filter reduce
Favicon
A light library for pipe style programming
Favicon
Ramda is your friend
Favicon
Why use pipe?
Favicon
3 lessons I learned getting started with Ramda
Favicon
Composing functions in JavaScript
Favicon
Mutating the immutable
Favicon
Building a React app with functional programming (Part 1)
Favicon
JavaScript Utility Libraries
Favicon
Working on the "Ramda Ramp-Up Guide"
Favicon
Grandma's recipes for cooking redux
Favicon
RamdaJS: transduce
Favicon
RamdaJS: Using it for the first time
Favicon
Why you should learn Functional Programming
Favicon
Handling objects with Ramda
Favicon
How Pipeline Operator makes your code cleaner
Favicon
Better Know A Method! with Ramda's .cond(), part 1
Favicon
Easily Integrate Ramda into Your React Workflow
Favicon
A pattern to write clean Redux connected components
Favicon
Ramda library - compose, map, sum
Favicon
Functional Lenses in Javascript with Ramda
Favicon
I ❤ Ramda - Partial Application with a Special Placeholder

Featured ones: