Struggling with my investigation methods a bit.
Would someone indicate where in this documentation it explains that the Boolean "method" will return only the truthy values please? So if I use it in the following code it returns the number of truthy values in array r. It does what it does but I need to understand why it doesn't return all truth/falsey values and only the truthy ones. Now I'm a bit more confused because I'm not sure if this question even makes sense...
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Boolean (edited)
17 Replies
Okay so would this work
the filter method needs a function
the filter method accepts a function, said function will be called with each one of the values you have on the array, if that function returns true then the value passes the filter and is added to the end result, otherwise is removed
No, as Erick said the filter method needs a function passed in. No function means
undefined
so it’ll filter out everything, as undefined is falseyI'm honestly surprised this even works
still, it's bad practice to write hard to read code, which this definitely qualifies as.
It’s called “points free” and isn’t that uncommon
filter((x) => Boolean(x))
is the same as
filter(Boolean)
Question concerning the earlier example: This makes sense now but it seems like mental gymnastics. So I guess my next question is that for me there is a huge gap between my understanding of filter() returning values that match my constraints VS understanding that basically filter() returns truthy values. Is this how it is for most everyone? And would this type of code knowledge come the same way I was introduced to it, by seeing an example, or does it all just become clear one day? I hope my question makes sense. My mind just struggles to get the concept, and hold onto that realization, much less being able to break it down into core concepts.
I think you're missing core concepts if this is confusing you to this level
.filter doesn't return truthy values, it returns values for which the function you pass in returns a truthy value
you're passing in Boolean as a function to call, which returns a truthy value when it's passed a truthy value, and a falsey value when it's passed a falsey value. It's a code golf shortcut to filter an array, that I wouldn't recommend you use yourself in production code. Write it out yourself, it's just
filter(x => x == true)
and it's much easier to readI do agree with Erick though, you probably want to read up and practice some more with the basics if this is so confusing. https://javascript.info is a good resource for that
Around 2019 there was an http request tool that was only able to execute from the frontend...or maybe it was only one the could work in both front and back and the rest were limited to the back? Anyone know?
ajax?
Fetch? Probably fetch, nodejs added support for it relatively recently. Also please make new posts for new questions
Thanks. Every question requires a post?! Seriously, does not qualify for a whole post, but the rules are the rules.
It does because a new post will attract new people to help you that already decided they couldn't help you with this one. It also makes it easier to find answers later by searching the forum posts
If you don't think it qualifies, 🤷