Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

238
Views
remove duplicates from an array when using laravel eloquent relationship in vuejs

So I have an array that has an object inside of it due to eloquent relationship in laravel. So the array looks something like this:

Documents:

[
    {
        "id": 6,
        "name": "document",
        "supplier": {
            "id": 5,
            "name": "H.B.C",
        }
    },
    {
        "id": 5,
        "name": "document",
        "supplier": {
            "id": 5,
            "name": "H.B.C",
        }
    },
    {
        "id": 4,
        "name": "document",
        "supplier": {
            "id": 4,
            "name": "Avrora",
        }
    }
]

Now I'm trying to use lodash to get unique suppliers so in the above example I would want to get back H.B.C and Avrora without another H.B.C.

What I'm trying:

CollectionSuppliers () {
    return uniq(this.Documents.supplier.map(({ name }) => name))
},

but im getting an error:

Cannot read properties of undefined (reading 'map')
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Cannot read properties of undefined (reading 'map')

This means whatever you're calling map on is undefined. You're calling it on:

this.Documents.supplier

so that means supplier is not a property of this.Documents. Which is true! Because this.Documents is an array of objects with a supplier, but the array itself doesn't have a supplier.

What you probably meant to do was:

return uniq(this.Documents.map(doc => doc.supplier.name))

This maps each document to just the supplier name, and then calls uniq on the array of supplier names.

over 4 years ago · Santiago Trujillo Report

0

You are accessing the supplier on the this.Documents object, but it doesn't exist, so it produces error

this.Documents.supplier.map(({ name }) => name)

You have to map over the this.Documents not on the this.Documents.supplier as:

CollectionSuppliers() {
  return uniq(this.Documents.map((o) => o.supplier.name));
}

or

CollectionSuppliers() {
  return uniq(this.Documents.map(({ supplier: { name } }) => name));
}

NOTE: You don't need lodash for this, you can get unique elements using vanilla JS also:

const Documents = [
  {
    id: 6,
    name: "document",
    supplier: {
      id: 5,
      name: "H.B.C",
    },
  },
  {
    id: 5,
    name: "document",
    supplier: {
      id: 5,
      name: "H.B.C",
    },
  },
  {
    id: 4,
    name: "document",
    supplier: {
      id: 4,
      name: "Avrora",
    },
  },
];

const result = [...new Set(Documents.map((o) => o.supplier.name))];
console.log(result);

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!