首页 > 数据库技术 > 详细

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

时间:2018-05-11 22:43:37      阅读:221      评论:0      收藏:0      [点我收藏+]

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`

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

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!