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

116
Views
Remove all objects with equal values from array of objects, excepted every first object

I need to leave in the array only objects with unique name values. Filter first unique objects. The rest must be removed. There's the array example:

var arr = [
  {name: "a", value: "1"},
  {name: "a", value: "2"},
  {name: "b", value: "1"},
  {name: "b", value: "2"},
  {name: "a", value: "3"},
  {name: "b", value: "3"},
  {name: "a", value: "4"},
  {name: "b", value: "4"},
  {name: "c", value: "5"},
]

I need to get something like that:

var arr = [
  {name: "a", value: "1"},
  {name: "b", value: "1"},
  {name: "c", value: "5"},
]

Would be grateful for the best way, using ES6 and newer

about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

Use reduce and Object.values to only accumulate the first of any given name

var arr = [
  {name: "a", value: "1"},
  {name: "b", value: "1"},
  {name: "a", value: "2"},
  {name: "b", value: "2"},
  {name: "a", value: "3"},
  {name: "b", value: "3"},
  {name: "a", value: "4"},
  {name: "b", value: "4"},
  {name: "c", value: "5"},
]

const result = Object.values(
    arr.reduce((acc, i) => (acc[i.name] ??= i, acc), {})
);

console.log(result);

about 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!