0

I have a List of objects. All of the objects contain a NAME property. There are several objects that have the same NAME property.

  const arr = [
    {name: "x", place: "a",  age: "13" },
    {name: "x", place: "b", age: "14" },
    {name: "y", place: "c",  age: "15" },
    {name: "d", place: "d", age: "16" }
]

How can I trim the List (or make a new List) where there is only one object per NAME property? Any other duplicates should be removed of the List

I should get a result like this

    const arr = [
    {name: "x", place: "a",  age: "13" },
    {name: "y", place: "c",  age: "15" },
    {name: "d", place: "d", age: "16" }
]

or

    const arr = [
    {name: "x", place: "b", age: "14" },
    {name: "y", place: "c",  age: "15" },
    {name: "d", place: "d", age: "16" }
]

can i do it with Lodash?

Omar EL KHAL
  • 151
  • 4
  • 13

1 Answers1

0

Lodash's uniqBy function is exactly what you need:

const _ = require('lodash');
const result = _.uniqBy(arr, 'name');
Mureinik
  • 297,002
  • 52
  • 306
  • 350