Last active
April 10, 2020 09:34
-
-
Save tianyk/62c175fd3ec9093fe43c05d44e245003 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function get(obj, fields) { | |
if (!obj) return; | |
return fields.split('.') | |
.reduce((obj, key) => obj[key], obj); | |
} | |
function set(obj, fields, val) { | |
if (!obj) return; | |
fields.split('.') | |
.reduce((obj, key, idx, arr) => { | |
// 避免 value 为空 | |
obj[key] = obj[key] || {}; | |
// 最后一个 field | |
if (idx === arr.length - 1) obj[key] = val; | |
return obj[key]; | |
}, obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment