Without getting into cryptic one-liners, there's a pretty straight forward linear time solution as well.
const seen = new Set(); const arr = [ { id: 1, name: "test1" }, { id: 2, name: "test2" }, { id: 2, name: "test3" }, { id: 3, name: "test4" }, { id: 4, name: "test5" }, { id: 5, name: "test6" }, { id: 5, name: "test7" }, { id: 6, name: "test8" } ]; const filteredArr = arr.filter(el => { const duplicate = seen.has(el.id); seen.add(el.id); return !duplicate; });
Hey Matt, nice solution! Yeah, all ways leads to Rome :)
Well... yes and no.
I feel it's important to distinguish that the OP-s solution loops over the whole array twice. I know there are times to make a trade-off between performance and readability, but I don't feel this needs to be one of those times :)
We're a place where coders share, stay up-to-date and grow their careers.
We strive for transparency and don't collect excess data.
re: Removing duplicates in an Array of Objects in JS with Sets VIEW POST
FULL DISCUSSIONWithout getting into cryptic one-liners, there's a pretty straight forward linear time solution as well.
Hey Matt, nice solution! Yeah, all ways leads to Rome :)
Well... yes and no.
I feel it's important to distinguish that the OP-s solution loops over the whole array twice. I know there are times to make a trade-off between performance and readability, but I don't feel this needs to be one of those times :)