I'm translating some Clojure
code to Javascript
for my job for transparency for the UI team and I realized why I prefer Clojure for Javascript. Here's the problem:
const myDistinctArray = [
...new Set(myObjArray.map(JSON.stringify)),
].map(JSON.parse);
or
// This one presumes unique "id" === unique object
let uniqueObjArray = [
...new Map(
objArray.map((item) => [item["id"], item])
).values(),
];
(distinct my-object-list) // that's it
Clojure promotes immutability by default, and this includes maps. So maps are compared directly by value and not by reference or identity.
The beautiful thing about Clojure is how expressive the language is and how much you can say with very few lines of code.
0
Thread