码迷,mamicode.com
首页 > 数据库 > 详细

[Javascript Crocks] Safely Access Nested Object Properties with `propPath`

时间:2018-05-11 23:47:33      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:SQ   fun   available   nested   mail   code   email   not   exists   

In this lesson, we’ll look at the propPath utility function. We’ll ask for a property multiple levels deep in an object and get back a Maybe. We’ll get a Just when the property exists at our path and a Nothing if any part of the path is undefined.

 

const propPath = require(crocks/Maybe/propPath)

const user = {
    username: tester,
    email: test@gmail.com,
    address: {
        street: 111 E. West St,
        city: Anywhere,
        state: PA,
        postalCode: 19123-4567
    }
};

const getPostalCode = propPath([address, postalCode]);
const zip = getPostalCode(user).option(not available);

console.log(zip) //‘19123-4567‘

 

[Javascript Crocks] Safely Access Nested Object Properties with `propPath`

标签:SQ   fun   available   nested   mail   code   email   not   exists   

原文地址:https://www.cnblogs.com/Answer1215/p/9026452.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!