Skip to content

Instantly share code, notes, and snippets.

@eugenserbanescu
Created June 27, 2017 14:30
Show Gist options
  • Save eugenserbanescu/ec2377c809bd57abf9d653945d70f11a to your computer and use it in GitHub Desktop.
Save eugenserbanescu/ec2377c809bd57abf9d653945d70f11a to your computer and use it in GitHub Desktop.
function getUndefinedKeys(checkObject, keys) {
return keys.filter(key => {
let splitKey = key.split('.');
let currentObj = checkObject;
let currentIndex = 0;
while(!isKeyUndefined(currentObj, splitKey[currentIndex]) && currentIndex < splitKey.length) {
currentObj = currentObj[splitKey[currentIndex]];
currentIndex++;
}
return currentIndex <= splitKey.length - 1;
});
}
function isKeyUndefined(obj, key) {
return !obj.hasOwnProperty(key) || obj[key] === undefined
}
var objToCheck = {
a: 1,
b: 2,
c: {
d: 3,
e: false
}
}
console.log('should be empty array:', getUndefinedKeys(objToCheck, ['a', 'b', 'c.d']));
console.log('should be empty array:', getUndefinedKeys(objToCheck, ['c']));
console.log('should be [c.f]:', getUndefinedKeys(objToCheck, ['a', 'b', 'c.f']));
console.log('should be [c.e.f]:', getUndefinedKeys(objToCheck, ['a', 'b', 'c.e.f']));
console.log('should be [e]:', getUndefinedKeys(objToCheck, ['e']));
console.log('should be empty array:', getUndefinedKeys(objToCheck, ['c.e']));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment