OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Remove duplicate from array of objects based on value of properties in JavaScript

  • Thread starter Thread starter Smarajit
  • Start date Start date
S

Smarajit

Guest
How can I remove duplicates from an array someArray like below based on the name property given the condition that if name is the same for two elements but for one them the type is new, the original one (without type new) will be retained?

someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}]

to

[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]

<p>How can I remove duplicates from an array <code>someArray</code> like below based on the <code>name</code> property given the condition that if <code>name</code> is the same for two elements but for one them the <code>type</code> is <code>new</code>, the original one (without type new) will be retained?</p>
<p><code>someArray = [{id: 1, name:"apple"}, {id: 2, name:"mango"}, {id: 3, name:"apple", type: "new"}, {id: 4, name:"orange"}, {id: 5, name:"orange", type: "new"}, {id: 6, name: "pineapple", type: "new"}] </code></p>
<p>to</p>
<p><code>[{id: 1, name:"apple"}, {id: 2, name: "mango"}, {id: 4, name:"orange"}, {id: 6, name: "pineapple", type: "new"}]</code></p>
 

Latest posts

I
Replies
0
Views
1
impact christian
I
Top