Skip to content

makes parity with native filter function#66

Open
kalpeshsingh wants to merge 1 commit intonitish24p:masterfrom
kalpeshsingh:patch-1
Open

makes parity with native filter function#66
kalpeshsingh wants to merge 1 commit intonitish24p:masterfrom
kalpeshsingh:patch-1

Conversation

@kalpeshsingh
Copy link
Copy Markdown

What was the issue?
The newFilter method allows undefined values to be part of the filtered array but the native implementation removes that.

How to replicate it?
Add numbers[7] = 9 and change condition to filter odd numbers and you will find out that it doesn't filter empty/undefined values in the array.

numbers array would be -

const numbers = [1, 2, 3, 4, undefined, undefined, undefined, 9]

What is the reason?
When we call callback.call(context, this[index], index, this) for undefined value then it produces true and it pushes the value to result array.

undefined => undefined % 2 !== 0 // produces `true`

How to prevent it?
We need to put a guard condition to only allow values that are actually part of the array and not produced artificially. The proposed condition makes sure that it matches with native .filter output.

The `newFilter` method allows undefined to be part of the filtered array but the native implementation removes that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant