@e-group/utils
Version:
eGroup team utils that share across projects.
30 lines (25 loc) • 567 B
JavaScript
import cloneDeep from 'lodash.clonedeep';
/**
* get value in object
*/
export default function getIn(obj, paths, defaultValue) {
let copy = obj;
let result;
for (let i = 0; i < paths.length; i++) {
const key = paths[i];
if (i === paths.length - 1) {
if (copy[key] != null) {
result = cloneDeep(copy[key]);
} else {
result = cloneDeep(defaultValue);
}
} else {
if (copy[key] == null) {
result = cloneDeep(defaultValue);
break;
}
copy = copy[key];
}
}
return result;
}