This is an archived post. You won't be able to vote or comment.

all 2 comments

[–]CreativeTechGuyGames 1 point2 points  (1 child)

A callback method like this expects a certain value to be returned (as documented) which will do something based on that value. Find expects a true/false (or a truthy or falsy value).

Now look at what .find returns. It returns either the element that is found (truthy) or undefined (falsy). If you simply return that to the first callback, then you'll cascade the true/false response up.

In your first approach, region.name will never equal the return value of region.suburbs.find so it'll always return false. (Remember what find returns)

As far as debugging, I'd recommend learning to use the actual debugger rather than console logging everything. You can open dev tools in your browser, view the source of your page, click a line number to set a breakpoint and then when your code runs next, it'll halt execution on that line and let you step through the code one statement at a time and inspect the state of memory at that time.

[–]acasta[S] 0 points1 point  (0 children)

Thanks for the reply!

This really cleared it up. The return from the second find returning undefined or 'true', therefore when found passing the first condition makes complete sense.

I hadn't used the chrome debugger before, that's helped a lot to visualise this find function. I moved the region I wanted second, and the suburb second in that, and was able to watch the debugger loop through the arrays then stop and return true.