File tree Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Expand file tree Collapse file tree 1 file changed +9
-6
lines changed Original file line number Diff line number Diff line change 1- const contains = function ( obj , searchValue ) {
2- const values = Object . values ( obj ) ;
1+ const contains = function ( initialObject , searchValue , queue = [ ] ) {
2+ if ( initialObject !== null ) queue . push ( initialObject ) ;
3+ if ( initialObject === null && queue . length === 0 ) return false ;
4+ const item = queue . shift ( ) ;
5+ const values = Object . values ( item ) ;
36
47 // NaN === NaN evaluates to false
58 // Normally, we would have to do an explicit Number.isNaN() check to compare NaN equality
69 // However, Array.prototype.includes automatically handles this for us
710 if ( values . includes ( searchValue ) ) return true ;
811
912 const nestedObjects = values . filter (
13+ // typeof null === 'object' evaluates to true ¯\_(ツ)_/¯
1014 ( value ) => typeof value === "object" && value !== null ,
1115 ) ;
12- for ( const nestedObject of nestedObjects ) {
13- return contains ( nestedObject , searchValue ) ;
14- }
1516
16- return false ;
17+ const newQueue = queue . concat ( nestedObjects ) ;
18+
19+ return contains ( null , searchValue , newQueue ) ;
1720} ;
1821
1922// Do not edit below this line
You can’t perform that action at this time.
0 commit comments