#ActuallyAutistic web dev. Does front of the front-end. Loves perf and minimalism. Prefers HTML, CSS, Web Standards over JS, UX over DX. Hates div disease.
Although that only "works" if the keys/elements are in the same order. It would also fail in any scenario where JSON.stringify() would fail - e.g., if the objects/arrays contain functions.
I did edit the original article slightly to indicate that it works with scalar (primitive) values. But I hadn't really thought of this as much of a "gotcha", because two objects that look the same... are not the same.
FWIW, this approach actually works with arrays and objects, if they are truly the same array/object. In other words, you can do something like this:
Does that work with array of objects, having similar properties?
No.
As Vesa demonstrated, this only works on scalar values. The reason is pretty simple:
You could kinda sorta get around this using the old
JSON.stringify()
hack:Although that only "works" if the keys/elements are in the same order. It would also fail in any scenario where
JSON.stringify()
would fail - e.g., if the objects/arrays contain functions.Came here to comment that. @bytebodger you might want to add that as a catch.
Because this particular gotcha has been a pain for me earlier.
I did edit the original article slightly to indicate that it works with scalar (primitive) values. But I hadn't really thought of this as much of a "gotcha", because two objects that look the same... are not the same.
FWIW, this approach actually works with arrays and objects, if they are truly the same array/object. In other words, you can do something like this:
And the resulting
noDupes
array will only contain one object. But if you do this:noDupes
will contain two objects. Because, even thoughanObject
andanotherObject
look the same to our eye, they are not the same value.