-
Notifications
You must be signed in to change notification settings - Fork 1
/
path.js
32 lines (28 loc) · 843 Bytes
/
path.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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