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.

  1. const R = require('ramda');
  2. const {path, pathOr} = R;
  3.  
  4. const acctDept = {
  5. name: 'Accounts Payable',
  6. location: '14th floor',
  7. personnel: {
  8. manager: {
  9. fName: 'Bill',
  10. lName: 'Lumberg',
  11. title: 'director of stuff and things',
  12. salary: 75000
  13. }
  14. }
  15. };
  16.  
  17. const itDept = {
  18. name: 'IT',
  19. location: 'remote',
  20. personnel: {}
  21. };
  22.  
  23. // path: will return undefined if cannot find prop
  24. const getMrgLastName = path(['personnel', 'manager', 'lName']);
  25. const getMrgLastNameOrDefaultVal = pathOr('Nobody', ['personnel', 'manager', 'lName'])
  26.  
  27. const res = getMrgLastName(acctDept);
  28. console.log("res:", res); // Lumberg
  29. const res2 = getMrgLastName(itDept);
  30. const res3 = getMrgLastNameOrDefaultVal(itDept);
  31. console.log("res2:", res2); // undefined
  32. console.log("res3:", res3); // Nobody

[Ramda] Get Deeply Nested Properties Safely with Ramda's path and pathOr Functions的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [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 ...

  4. [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 ...

  5. 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 ...

  6. coffeescript 1.8.0 documents

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  7. how to use coffee script

    TABLE OF CONTENTS TRY COFFEESCRIPT ANNOTATED SOURCE CoffeeScript is a little language that compiles ...

  8. coffeescript 1.6.3使用帮助

    CoffeeScript is a little language that compiles into JavaScript. Underneath that awkward Java-esque ...

  9. ElasticSearch学习问题记录——nested查询不到数据

    通过代码创建了索引名称为demoindex,索引类型为school,以下是索引类型的数据映射结构: { "state": "open", "setti ...

随机推荐

  1. 【Codeforces Round #453 (Div. 2) C】 Hashing Trees

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 显然只有当a[i]和a[i-1]都大于1的时候才会有不同的情况. a[i] >= a[i-1] 且a[i-1]>=2 则 ...

  2. 设计模式六大原则(五):迪米特法则(Law Of Demeter)

    定义: 一个对象应该对其他对象保持最少的了解. 问题由来: 类与类之间的关系越密切,耦合度越大,当一个类发生改变时,对另一个类的影响也越大. 解决方案: 尽量降低类与类之间的耦合. PS: 自从我们接 ...

  3. android对话框(Dialog)的使用方法

    Activities提供了一种方便管理的创建.保存.回复的对话框机制.比如 onCreateDialog(int), onPrepareDialog(int, Dialog), showDialog( ...

  4. Node.js笔记 http fs

    const http=require('http'); const fs=require('fs'); var server = http.createServer(function(req, res ...

  5. oracle-function 练习

    /* *scm_iss.test_imti_fun2 *带有输入參数的Function */ CREATE OR REPLACE FUNCTION TEST_IMTI_FUN2(P_NO IN NUM ...

  6. Session丢失原因与解决方案

    win2003 server下的IIS6默认设置下对每个运行在默认应用池中的工作者进程都会经过20多个小时后自动回收该进程,   造成保存在该进程中的session丢失. 因为Session,Appl ...

  7. 内存、时间复杂度、CPU/GPU以及运行时间

    衡量 CPU 的计算能力: 比如一个 Intel 的 i5-2520M @2.5 Ghz 的处理器, 则其计算能力 2.5 * 4(4核) = 10 GFLOPS FLOP/s,Floating-po ...

  8. 每日技术总结:filter(),Bscroll

    前言: 这是一个vue的电商项目,使用express后端提供数据. 1.filter()函数. 事情是这样的.我从数据库拿到了所有分类数据. 分类有三个等级.父类,子类,孙类这样.但它们都在同一张表里 ...

  9. (转)把Sublime Text 2 加入右键菜单(带图标),Edit with Sublime Text

    转自 http://www.turen.me/archives/509 Sublime Text 2 是现在很受大家欢迎的编辑器了,不仅是在web前端,在书定简单的php.Js等代码时,也是相当的好用 ...

  10. Android layer-list的属性和使用具体解释

    Android layer-list的属性和使用具体解释.layer-list是用来多个图层堆叠显示的,借这个特性能够做一些特别的效果(比方:阴影.以下的效果等),也能够投机取巧. 1.代码片 < ...