r/learnjavascript • • May 14 '21

Remove Duplicate Value from array

Post image
234 Upvotes

89 comments sorted by

View all comments

20

u/slowthedataleak May 14 '21

Does this perform better than filter()?

2

u/47milliondollars May 14 '21

Would filter even work? Or do you have to use reduce to check if the value exists yet in an accumulator?

7

u/space_scarab May 14 '21
arr.filter((el, index) => {
    return arr.indexOf(el) === index;
});

10

u/[deleted] May 14 '21 edited May 14 '21
arr.filter((el, index) => arr.indexOf(el) === index);

Works too!

(but worth saying that shorthand isn't always the most readable and shouldn't be your default approach!)

3

u/[deleted] May 15 '21

Honestly that shorthand is way easier to parse - for me at least

2

u/StoneCypher May 14 '21

is there any difference there other than switching to an arrow?

0

u/[deleted] May 14 '21

[deleted]

-5

u/StoneCypher May 14 '21

Thanks, I didn't ask what an arrow was.

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.

7

u/47milliondollars May 14 '21

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.

-3

u/StoneCypher May 14 '21

(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.

4

u/amoliski May 14 '21

Doesn't hurt to show both forms so if someone isn't familiar with them they can see they are similar.

→ More replies (0)

1

u/grooomps May 15 '21

So a function without a fat arrow doesn't have explicit return?

1

u/space_scarab May 14 '21

ah, yes, cleaner. thanks!