Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Remove same property value duplicate from array of objects using Lodash

I have the below array

const values = [
  {
    name: 'A',
    age: 12
  },
  {
    name: 'B',
    age: 1
  },
  {
    name: 'A',
    age: 31
  }
]

and I want using lodash remove an object from this array that has the same name, so it should return:

 const values = [
  {
    name: 'A',
    age: 12
  },
  {
    name: 'B',
    age: 1
  }
]

Using

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

_.uniqWith(_.isEqual, values) 

only remove duplicate if the whole object is duplicated …
How can I remove using property?

>Solution :

Use _.uniqBy() the name property instead:

const values = [{"name":"A","age":12},{"name":"B","age":1},{"name":"A","age":31}]

const result = _.uniqBy(values, 'name')

console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js" integrity="sha512-WFN04846sdKMIP5LKNphMaWzU7YpMyCU245etK3g/2ARYbPK9Ub18eG+ljU96qKRCWh+quCY7yefSmlkQw1ANQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading