Created
September 15, 2016 19:09
-
-
Save iSkore/625ba625bc687d5d2fba86785fc45960 to your computer and use it in GitHub Desktop.
Get all values for key
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 getAllWithKey( o, k ) { | |
return new Promise( ( res, rej ) => { | |
let paths = [], | |
vals = [], | |
find = obj => { | |
return new Promise( r => { | |
let run = oa => _.forEach( oa, ( v, n ) => { | |
if( _.has( v, k ) ) { | |
vals.push( v[ k ] ); | |
_.unset( v, paths ); | |
return run( v[ paths.pop() ] ); | |
} else { | |
if( _.isObject( v ) ) { | |
paths.push( n ); | |
return run( v ); | |
} else return run( v[ paths.pop() ] ); | |
} | |
} ); | |
r( run( obj ) ); | |
} ); | |
}; | |
if( _.isString( k ) && _.isObject( o ) ) res( find( o ).then( () => ( vals ) ) ); | |
else rej( 'Argument Error - Must have an object and a string' ); | |
} ); | |
} | |
getAllWithKey( lobj, 'resourceId' ).then( console.log ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment