Skip to content

Instantly share code, notes, and snippets.

@iSkore
Created September 15, 2016 19:09
Show Gist options
  • Save iSkore/625ba625bc687d5d2fba86785fc45960 to your computer and use it in GitHub Desktop.
Save iSkore/625ba625bc687d5d2fba86785fc45960 to your computer and use it in GitHub Desktop.
Get all values for key
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