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的更多相关文章

  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. MyBatis学习总结(12)——Mybatis+Mysql分页查询

    package cn.tsjinrong.fastfile.util; /**  * @ClassName: Page  * @Description: TODO(分页组件的父类,用来封装分页的 通用 ...

  2. 词向量 word2vec

    看的这一篇的笔记 http://licstar.net/archives/328 看不太懂. 要学的话,看这里吧,这里把一些资料做了整合: http://www.cnblogs.com/wuzhitj ...

  3. enq: TX - row lock contention故障处理一则

    一个非常easy的问题,之所以让我对这个问题进行总结.一是由于没我想象的简单,在处理的过程中遇到了一些磕磕碰碰,甚至绕了一些弯路.二是引发了我对故障处理时的一些思考. 6月19日,下午5点左右.数据库 ...

  4. [Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript

    Vue 2.2 introduced a simple dependency injection system, allowing you to use provide and inject in y ...

  5. hbs模板(zmaze ui用的)

    hbs模板(zmaze ui用的) 一.总结 1.模板引擎:就是来生成界面的啊,只不过实现了view和数据分离以及一些其它的功能(预加载等). 2.Handlebars :但他是一个单纯的模板引擎,在 ...

  6. ASP.net 环境搭建

    https://www.cnblogs.com/leizhanjun/p/6081928.html

  7. 1.IntelliJ IDEA搭建SpringBoot的小Demo

    转自:http://www.cnblogs.com/weizaibug/p/6657077.html 首先简单介绍下Spring Boot,来自度娘百科:Spring Boot是由Pivotal团队提 ...

  8. Android Studio运行报错,Cannot find System Java Compiler. Ensure that you have installed a JDK......

    详细报错信息如下 Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Cannot find System J ...

  9. 洛谷 P1727 计算π

    P1727 计算π 题目背景 <爱与愁的故事第二弹·compute>第一章. 题目描述 中秋至,博饼声铿锵不断.爱与愁大神兴致勃勃地到学校博饼,结果抱回家的只有一秀二举.爱与愁大神十分生气 ...

  10. Android Studio - no debuggable applications 的解决的方法

    之前logcat总是无法显示调试应用的信息 曾经我都是卸载重装.后来发如今StackOverflow有一个哥们说的非常对.一次就成功. 原话是这么说的: You also should have To ...