Skip to content

Instantly share code, notes, and snippets.

@tianyk
Last active April 10, 2020 09:34
Show Gist options
  • Save tianyk/62c175fd3ec9093fe43c05d44e245003 to your computer and use it in GitHub Desktop.
Save tianyk/62c175fd3ec9093fe43c05d44e245003 to your computer and use it in GitHub Desktop.
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