Skip to content

Simplify array_unique#3

Open
timmarinin wants to merge 1 commit intoamitmerchant1990:masterfrom
timmarinin:patch-1
Open

Simplify array_unique#3
timmarinin wants to merge 1 commit intoamitmerchant1990:masterfrom
timmarinin:patch-1

Conversation

@timmarinin
Copy link
Copy Markdown

Use Array.prototype.indexOf instead of custom key comparator.

This change probably makes function slower due to indexOf probable O(n) timing,
but clarity of the code is more important than the performance in this case.

Use Array.prototype.indexOf instead of custom key comparator.

This change probably makes function slower due to indexOf probable O(n) timing,
but clarity of the code is more important than the performance in this case.
Copy link
Copy Markdown
Owner

@amitmerchant1990 amitmerchant1990 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It failed to pass the following case:

array_unique([{a: 'val'}, {b: 'val', c: 'val'}, 'a', 'b', [1,2,3,4], {a: 'val'}, [1,2,3,4], [{a: 'val'}, {b: 'val'}], [{a: 'val'}, {b: 'val'}]]);
// [{a: 'val'}, {b: 'val', c: 'val'}, 'a', 'b', [1,2,3,4], [{a: 'val'}, {b: 'val'}]]

Can you please check?

@atk
Copy link
Copy Markdown

atk commented Sep 22, 2017

indexOf will only compare the content's indices, not it's contents if those are objects, arrays or NaN. Therefore, this breaks the current functionality.

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.

3 participants