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

231
Views
Remove duplicate objects with condition

Suppose I have a list of objects like this:

let b = [
  {
    name: "test1", 
    connectedTo: "",
  },
  {
    name: "test1",
    connectedTo: "test1.test2.test3"
  },
  {
    name: "test2",
    connectedTo: "",
  },
  {
    name: "test3",
    connectedTo: "",
  }
]

I want to get elements without duplicates name and also if there are duplicates take one without empty connectedTo. So from example above the result that I expect is:

let result = [
  {
    name: "test1",
    connectedTo: "test1.test2.test3"
  },
  {
    name: "test2",
    connectedTo: "",
  },
  {
    name: "test3",
    connectedTo: "",
  }
]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Here's an approach utilizing Array#reduce function:

let b = [
  {
    name: "test1", 
    connectedTo: "",
  },
  {
    name: "test1",
    connectedTo: "test1.test2.test3"
  },
  {
    name: "test2",
    connectedTo: "",
  },
  {
    name: "test3",
    connectedTo: "test1.test2",
  },
  {
    name: "test3",
    connectedTo: "",
  }
];

const result = Object.values(b.reduce((acc, cur) => {
  if(!acc[cur.name] || !acc[cur.name].connectedTo)
    acc[cur.name] = cur;
  return acc;
}, {}));

console.log(result);

about 4 years ago · Juan Pablo Isaza 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!