Created
April 24, 2022 03:38
This file contains 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
function isObject(object) { | |
return object != null && object.constructor.name === 'Object'; | |
} | |
function isArray(object) { | |
return object != null && object.constructor.name === 'Array'; | |
} | |
/** | |
* Find all values corresponding to a matching property. | |
* | |
* @param property | |
* @param object | |
* @param values | |
* @returns {*[]} | |
*/ | |
function findAllValuesOfMatchingProperty(property, object, values = []) { | |
if (isArray(object)) { | |
object.forEach(value => { | |
return findAllValuesOfMatchingProperty(property, value, values); | |
}); | |
} | |
if (isObject(object)) { | |
Object.entries(object).forEach(([key, value]) => { | |
if (key.match(property)) values.push(value); | |
return findAllValuesOfMatchingProperty(property, value, values); | |
}); | |
} | |
return values; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment