[Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions
In this lesson we'll see how Ramda's path
and pathOr
functions can be used to safely access a deeply nested property from an object while avoiding the dreaded checks for undefined
at each new property in the desired path.
- const R = require('ramda');
- const {path, pathOr} = R;
- const acctDept = {
- name: 'Accounts Payable',
- location: '14th floor',
- personnel: {
- manager: {
- fName: 'Bill',
- lName: 'Lumberg',
- title: 'director of stuff and things',
- salary: 75000
- }
- }
- };
- const itDept = {
- name: 'IT',
- location: 'remote',
- personnel: {}
- };
- // path: will return undefined if cannot find prop
- const getMrgLastName = path(['personnel', 'manager', 'lName']);
- const getMrgLastNameOrDefaultVal = pathOr('Nobody', ['personnel', 'manager', 'lName'])
- const res = getMrgLastName(acctDept);
- console.log("res:", res); // Lumberg
- const res2 = getMrgLastName(itDept);
- const res3 = getMrgLastNameOrDefaultVal(itDept);
- console.log("res2:", res2); // undefined
- console.log("res3:", res3); // Nobody
[Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions的更多相关文章
- [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method
Among the features introduced to the language of JavaScript in ES2019 is Array.prototype.flat. In th ...
- [Ramda] Pick and Omit Properties from Objects Using Ramda
Sometimes you just need a subset of an object. In this lesson, we'll cover how you can accomplish th ...
- [Ramda] Eliminate Function Arguments (Point-Free Style) with Ramda's Converge
When doing comparisons inside of functions, you end of relying heavily on the argument passed into t ...
- [Ramda] Count Words in a String with Ramda's countBy and invert
You can really unlock the power of ramda (and functional programming in general) when you combine fu ...
- vue中引入mui报Uncaught TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed on strict mode functions or the arguments objects for calls to them的错误
在vue中引入mui的js文件的时候,报如下的错误: 那是因为我们在用webpack打包项目时默认的是严格模式,我们把严格模式去掉就ok了 第一步:npm install babel-plugin-t ...
- coffeescript 1.8.0 documents
CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...
- how to use coffee script
TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...
- coffeescript 1.6.3使用帮助
CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...
- ElasticSearch学习问题记录——nested查询不到数据
通过代码创建了索引名称为demoindex,索引类型为school,以下是索引类型的数据映射结构: { "state": "open", "setti ...
随机推荐
- 【Codeforces Round #453 (Div. 2) C】 Hashing Trees
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有当a[i]和a[i-1]都大于1的时候才会有不同的情况. a[i] >= a[i-1] 且a[i-1]>=2 则 ...
- 设计模式六大原则(五):迪米特法则(Law Of Demeter)
定义: 一个对象应该对其他对象保持最少的了解. 问题由来: 类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案: 尽量降低类与类之间的耦合. PS: 自从我们接 ...
- android对话框(Dialog)的使用方法
Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...
- Node.js笔记 http fs
const http=require('http'); const fs=require('fs'); var server = http.createServer(function(req, res ...
- oracle-function 练习
/* *scm_iss.test_imti_fun2 *带有输入參数的Function */ CREATE OR REPLACE FUNCTION TEST_IMTI_FUN2(P_NO IN NUM ...
- Session丢失原因与解决方案
win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程, 造成保存在该进程中的session丢失. 因为Session,Appl ...
- 内存、时间复杂度、CPU/GPU以及运行时间
衡量 CPU 的计算能力: 比如一个 Intel 的 i5-2520M @2.5 Ghz 的处理器, 则其计算能力 2.5 * 4(4核) = 10 GFLOPS FLOP/s,Floating-po ...
- 每日技术总结:filter(),Bscroll
前言: 这是一个vue的电商项目,使用express后端提供数据. 1.filter()函数. 事情是这样的.我从数据库拿到了所有分类数据. 分类有三个等级.父类,子类,孙类这样.但它们都在同一张表里 ...
- (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text
转自 http://www.turen.me/archives/509 Sublime Text 2 是现在很受大家欢迎的编辑器了,不仅是在web前端,在书定简单的php.Js等代码时,也是相当的好用 ...
- Android layer-list的属性和使用具体解释
Android layer-list的属性和使用具体解释.layer-list是用来多个图层堆叠显示的,借这个特性能够做一些特别的效果(比方:阴影.以下的效果等),也能够投机取巧. 1.代码片 < ...