You generally don't need forEach, a regular old for loop looks nicer. You also don't need the if; it's not harmful to write the same key twice.
for(letxofarray){x[v]=true;}
However, it's important to highlight the differences between these implementations. This one, for example, only works when your array contains only strings.
I really can't condone using map as forEach. A more interesting variant would use spreading with reduce, like
array.reduce((x,v)=>({[v]:true,...x}),{})
but again this has the limitation that it only works with strings.
How do you implement "array.reduce((x, v) => ({[v]: true, ...x}), {})" in a removeDuplicates function? Im unfamiliar with that syntax.
Im running performance tests and so far this function is the fastest one:
function removeDuplicates3(array) {
array.splice(0, array.length, ...(new Set(array)))
}
but I want to test your suggestion too.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
You generally don't need
forEach
, a regular oldfor
loop looks nicer. You also don't need theif
; it's not harmful to write the same key twice.However, it's important to highlight the differences between these implementations. This one, for example, only works when your array contains only strings.
I really can't condone using
map
asforEach
. A more interesting variant would use spreading withreduce
, likebut again this has the limitation that it only works with strings.
How do you implement "array.reduce((x, v) => ({[v]: true, ...x}), {})" in a removeDuplicates function? Im unfamiliar with that syntax.
Im running performance tests and so far this function is the fastest one:
function removeDuplicates3(array) {
array.splice(0, array.length, ...(new Set(array)))
}
but I want to test your suggestion too.