dev-resources.site
for different kinds of informations.
Ramda is your friend
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"
}
]
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"]]
Link to Ramda - https://ramdajs.com/
Enjoy 😊
Featured ones: