0
Please help me!!!
Searching through deep nested object using recursions. https://code.sololearn.com/cA9a5a11a9A7/?ref=app
10 Answers
+ 2
two possible solutions:
first one return true if first argument is not an object and equals second argument...
function contains(obj, item) {
return typeof obj=="object" ?
Object.entries(obj).some(
([k,v]) => contains(v, item)
) : obj == item;
}
second one return undefined if first argument is not an object, else return true or false according to if one nested value / object contains second argument:
function contains(obj, item) {
if (typeof(obj)!="object") return;
return Object.entries(obj).some(([k,v]) =>
typeof v=="object" ?
contains(v, item) : v == item
);
}
if you want to compare values with item strictly (no automatic cast when comparing -- ie: 42 !== "42"), use triple equal sign to compare obj/v against item, rather than double equal sign...
+ 2
visph I'm now seeing this. I never noticed! So sorry bro. The best mark also that I removed was a mistake. It wasn't intentional!
+ 2
visph but why would I remove it on purpose? You answered well and it helped me a lot. I wouldn't be that ungrateful!
+ 1
visph my good friend. Please Another question. These days I've been learning a lot about recursions and solving many questions online.
+ 1
visph thanks a million!!! , good friend ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️ ☺️
+ 1
visph hi. It keeps giving me "false" regardless of the item as argument
+ 1
my bad: I've too quickly turned my versions using Object.keys() to version nearest to your using Object.entries() ^^
previous answer corrected: you just need to put "k,v" inside square brackets to destructure value passed as callback argument of some() method ;)
+ 1
visph it worked now!! 🎊 🎊 🎊 😄😄😄😄😄😄. Thanks again 😊😊😊😊
+ 1
why removing best answer mark???
+ 1
well, I hope you're not lying (it's difficult to believe that it was a mistake)... and that you will not remove it again later (or delete the thread): I'm tired of user who set and remove best answer mark (or delete thread) without reasons, even if I help those who didn't set best answer at all ^^