Oh holy shit, never thought about this. Looks like the indexOf is finding the first match, and so the filter drops anything of the same value that isn't at the first detected index? Pretty neat.
What I asked was "is that the only change you made"
Notably, you shouldn't have that "auto-corrected," because arrows and standard functions differ in how they bind this. That should be hand-corrected. In many cases in practice, this will damage transitional-style classes.
Dude, way to be a jerk to someone who kindly took the time to respond to that question (why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?). Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.
(why would they think you were asking if they made any other changes when you're looking right at the 1-3 lines of code?).
The point of the original question was to ask why this person is holding up a non-change as a change. I was trying to make sure I wasn't overlooking something else more substantive.
Also, their response was perfectly accurate for this code example we are looking at, the context of "this" is totally irrelevant here.
Yes, I'm ... kind of relying on that.
Literally the only difference between the two pieces of code is something that is totally irrelevant here.
It's like taking out the newlines and "proposing it as an alternative." Literally.
Man. I wish i can think like this. How do you do ? I was stuck at this smilar knida problem (find if array contains duplicate value or not and just return true or false.) and it never clicked me.
First, this approach will not work for non-primitive values. If you're dealing with an array of arrays or an array of objects or even an array of functions, it'll just compare their references. So if the array has two objects that have the same contents but were declared separately, you'll still get values that look like duplicates. That might be just fine for some applications, but if it's not, you'll have to do a deeper comparison.
Second, is this actually better? This isn't just rhetorical; I'm actually asking. Filtering is O(n), but so is indexOf, so since the indexOf call is inside the filter, the whole operation is O(n2). Meanwhile, generating a set is also O(n), as is destructing it into an array. But since they're done back-to-back and not inside each other, it's O(2n). I know that the highest factor doesn't always tell the whole story, but are there times when the set approach would perform better?
7
u/space_scarab May 14 '21