Hi Tony. Your answer helped me, thanks!
BTW, can you please help me achieve this array?
[
{ id: 1, name: "test1" },
{ id: 2, name: "test2" },
{ id: 2, name: "test3" },
{ id: 3, name: "test2" }
]
I mean even if 'id' or 'name' already exists, it should not be omitted because either of the value is different (like in the case of 'name: "test2"') in the whole array.
I'm not sure I follow. If no duplicates are removed then that is just the original array is it not? Or do you mean you want an array with just ids 1, 2, and 3? If so, you can use Array.prototype.filter and only return true for the ids you want to keep.
Might be late with a reply here, but in your case I would probably map through your array of objects as stringified content, as you are not relying on a single identifier but an entire object.
NB: Though this might not be performant when you are dealing with bigger objects and large arrays.
There is a difference between Tony's and Matt's approach in how the final array will look like.
Matt's approach is adding the id for each entry it loops through to a Set and checks whether or not it has been ´seen´ before or not, and return the object if 'no' is the case. So if we look at the returned object with ID: 2, Matt will return the object with name: "test2" as it will consider the next object a duplicate and skip it.
Tony's approach is by creating a new map using ID as a key - which has to be unique - and then extracts the values. E.g. [1: { id: 1, name: "test1" }, 2: { id: 2, name: "test2" }....] etc. What this means though, is that even though id: 2 has been added to the map, it is simply overwritten by the third item in the array, thus Tony will return name: "test3" for ID: 2.
Just keep this in mind whether you want the first object or the last object by a duplicated identifier to be the truth.
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.
Here's another possibility using the
Map
class constructor andvalues
method:wow, i love that
Thanks !!
Hi Tony. Your answer helped me, thanks!
BTW, can you please help me achieve this array?
[
{ id: 1, name: "test1" },
{ id: 2, name: "test2" },
{ id: 2, name: "test3" },
{ id: 3, name: "test2" }
]
I mean even if 'id' or 'name' already exists, it should not be omitted because either of the value is different (like in the case of 'name: "test2"') in the whole array.
Hi Mayank,
I'm not sure I follow. If no duplicates are removed then that is just the original array is it not? Or do you mean you want an array with just
id
s 1, 2, and 3? If so, you can use Array.prototype.filter and only returntrue
for theid
s you want to keep.Hey @mayanxoni ,
Might be late with a reply here, but in your case I would probably map through your array of objects as stringified content, as you are not relying on a single identifier but an entire object.
NB: Though this might not be performant when you are dealing with bigger objects and large arrays.
Great
I have to create an account and say thank you! This answer save my day.
There is a difference between Tony's and Matt's approach in how the final array will look like.
Matt's approach is adding the id for each entry it loops through to a Set and checks whether or not it has been ´seen´ before or not, and return the object if 'no' is the case. So if we look at the returned object with ID: 2, Matt will return the object with name: "test2" as it will consider the next object a duplicate and skip it.
Tony's approach is by creating a new map using ID as a key - which has to be unique - and then extracts the values. E.g.
[1: { id: 1, name: "test1" }, 2: { id: 2, name: "test2" }....]
etc. What this means though, is that even though id: 2 has been added to the map, it is simply overwritten by the third item in the array, thus Tony will return name: "test3" for ID: 2.Just keep this in mind whether you want the first object or the last object by a duplicated identifier to be the truth.