Created
September 26, 2025 16:46
-
-
Save davidalves1/fbba8fc660266dd42a09d651f712efa0 to your computer and use it in GitHub Desktop.
Javascript Helper Functions
This file contains hidden or 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
// Retrieves a nested property from an object using a dot-notation key string. | |
const getDeepPropertyObject = (key, obj) => { | |
const steps = key.split('.') | |
return steps.reduce((curr, key) => { | |
if (!curr) { | |
return obj[key] | |
} | |
return curr[key] | |
}, null) | |
} | |
// Example | |
console.log(getObjectItem('user.address.street', { | |
user: { | |
name: 'David', | |
address: { | |
street: 'Rua AB', | |
number: 100 | |
} | |
} | |
})) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment