-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.js
More file actions
37 lines (30 loc) · 883 Bytes
/
test.js
File metadata and controls
37 lines (30 loc) · 883 Bytes
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
33
34
35
36
37
//console.log({} === undefined);
let obj = { "repository": {} };
//console.log(obj['repository']['readme']);
function safelyGet(object, path) {
const pathArr = path.split('.');
let result = object;
for(let i=0; i<pathArr.length; i+=1){
// console.log("el:", el);
// console.log("result el:", result[el] === undefined);
if(result[pathArr[i]] === undefined){
return undefined;
}
else {
result = result[pathArr[i]];
}
}
// pathArr.map((el) => {
// console.log("el:", el);
// console.log("result el:", result[el] === undefined);
// if(result[el] === undefined){
// return undefined;
// }
// else {
// result = result[el];
// }
// });
return result;
// 입력해주세요
}
console.log(safelyGet(obj, "repository.readme"));