javascript - ES6 / lodash - check nested Object values with startsWith -
is there function or fast way check if value in our object startswith e.g asd
example:
let obj = { 'child' : { 'child_key': 'asdfghhj' }, 'free': 'notasd', 'with': 'asdhaheg' } // check here if our obj has value startswith('asd')
regards
you can recursively iterate object properties , check if property starts prefix
using find
function:
function haspropertystartingwith(obj, prefix) { return !!object.keys(obj).find(key => { if (typeof obj[key] === 'object') { return haspropertystartingwith(obj[key], prefix) } if (typeof obj[key] === 'string') { return obj[key].startswith(prefix) } return false }) } console.log(haspropertystartingwith(obj, 'asd'))
Comments
Post a Comment