verificator
Version:
Client and server-side validation JavaScript library
15 lines (14 loc) • 430 B
JavaScript
const get = (object, key, defaultValue) => {
if (object == null) {
return defaultValue;
}
let path = key.split('.');
let index = 0;
const length = path.length;
while (object != null && index < length) {
object = object[path[index++]];
}
const result = (index && index === length) ? object : undefined;
return result === undefined ? defaultValue : result;
};
export default get;