Comments (12)
I think JSON.stringify
is not the best choice for deeply comparing two objects, It is not going to work as expected for function and symbol types and some others, so it will have different behavior for different types of objects and you should have different strategy based on types of objects. I think using ===
is better because it will always return false for objects that do not have the same reference.
from toolbox.
in my opinion uniqBy
comes very handy, but uniq
and uniqWith
can be implemented in just one function (for default using shallow comparison otherwise using a provided comparison function).
from toolbox.
How should elements be compared with each other? through deep comparison or just by reference?
from toolbox.
see this example: https://codesandbox.io/p/sandbox/uniq-example-czqq98
@hamed-bavar @ASafaeirad
from toolbox.
please share a sandbox
from toolbox.
check this out. https://playcode.io/1562822
from toolbox.
This is good, but does not work on an array of objects. How we can provide this on your implementation?
anotherImplementation([
{name:"John"},
{name:"John"},
])
from toolbox.
I know, as I said this implementation is just a shallow comparison (===) for the sake of keeping everything simple and performant [just like uniq function in lodash]. For deep comparison you need more than that, just a JSON.stringify. the uniq function should be prepared for comparing functions, dates, Symbols, complex nested objects, etc which makes the function very messy and slow.
from toolbox.
@ASafaeirad, @mahdi-sheibak. what if we shallow compare by default unless a comparison function was provided. but the time complexity would be more than previous implementation since the compare function of sets in JavaScript can not be changed and we need to use array instead.
from toolbox.
I know, as I said this implementation is just a shallow comparison (===) for the sake of keeping everything simple and performant [just like uniq function in lodash]. For deep comparison you need more than that, just a JSON.stringify. the uniq function should be prepared for comparing functions, dates, Symbols, complex nested objects, etc which makes the function very messy and slow.
I completely agree with you! How about we take inspiration from lodashjs and create three useful functions: 'uniq' to filter out duplicate values, 'uniqBy' to filter duplicates by a specific key, and 'uniqWith' to filter duplicates based on custom comparison logic?
from toolbox.
Agree, two objects with different references are not considered the same in the language therefore, to follow the "Least Surprise" principle, the uniq
function should not mark them as duplicates. let's keep it simple and compare it with Object.is
from toolbox.
🎉 This issue has been resolved in version 4.5.0 🎉
The release is available on:
Your semantic-release bot 📦🚀
from toolbox.
Related Issues (20)
- Typescript 5 ESM Support HOT 1
- Documentation HOT 1
- Test and coverage CI
- tryCatch + nullableTryCatch HOT 8
- isNull Guard on Nested Nullable Chain HOT 1
- The automated release is failing 🚨 HOT 4
- AsyncVoidFn
- Code Coverage Badge
- [Proposal]: debounce HOT 4
- [Proposal]: throttle HOT 3
- [Proposal]: toggleArrayValue HOT 4
- [Proposal]: toDecimal fallback
- [Proposal]: merge HOT 3
- [Bug]: External `tryOr` link in the docs HOT 1
- [Proposal]: bindArgs (only-args, exclude `thisArg`) HOT 2
- [Proposal]: Camel case maker in order to format Json/data object keys that we get from backend HOT 1
- [Proposal]: format minutes into a nice time format HOT 3
- [Proposal]: Format price in a easy way HOT 1
- working with params and queries in the url
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from toolbox.