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. JSP页面开发规范案例

    添加 <%@ page language="java" contentType="text/html; charset=utf-8" pageEncodi ...

  2. Android学习路线(十四)Activity生命周期——停止和重新启动(Stopping and Restarting)一个Activity

    正确地停止和重新启动你的activity在activity的生命周期中是一个非常重要的过程.这样可以确保你的用户感觉到你的应用一直都活着而且没有丢失进度.你的activity的停止和重新启动时有几个重 ...

  3. Google Web Toolkit(GWT) 在windows下环境搭建

    1.什么是GWT? Google Web Toolkit(简称GWT,读作/ˈɡwɪt/),是一个前端使用JavaScript,后端使用Java的AJAX framework,以Apache许可证2. ...

  4. iptables转发安卓手机热点的数据到指定的端口

    iptables转发安卓手机热点的数据到指定的端口 手机安装了VPN,可以上GOOGLE的那种.然后我打开手机的热点,连上笔记本,想让本本上个google 没想到被GFW挡住了.看了一下手机的网络工作 ...

  5. LayoutAnimation-容器动画

    1.LayoutAnimation的作用主要就是加载到一个layout上,让这个layout里面的所有控件都有相同的动画效果.现在用到的是在listview中添加动画,使得它每一个item都是滑落显示 ...

  6. js进阶 14-7 jquery的ajax部分为什么需要对表单进行序列化

    js进阶 14-7 jquery的ajax部分为什么需要对表单进行序列化 一.总结 一句话总结:如果用ajax传递表单的数据,如果不进行表单的序列化,要一个参数一个参数的写,太麻烦,序列化的话,一句代 ...

  7. .condarc(conda 配置文件)

    Configuration - Conda documentation .condarc以点开头,一般表示 conda 应用程序的配置文件,在用户的家目录(windows:C:\\users\\use ...

  8. python之字符串 元祖 列表 字典

    一 字符串操作 语法:' ' 类型:str #首字母大写其余全部小写 test1 = 'yanShichenG' v = test1.capitalize() #全部小写(可以处理特殊字符) v1 = ...

  9. Java网络编程之TCP、UDP

    Java网络编程之TCP.UDP 2014-11-25 15:23 513人阅读 评论(0) 收藏 举报 分类: java基础及多线程(28) 版权声明:本文为博主原创文章,未经博主允许不得转载.   ...

  10. query中prop()方法和attr()方法的区别

    query1.6中新加了一个方法prop(),一直没用过它,官方解释只有一句话:获取在匹配的元素集中的第一个元素的属性值. 官方例举的例子感觉和attr()差不多,也不知道有什么区别,既然有了prop ...