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

149
Views
filter array so that objects with same property only appear once in array

I have following array:

[
  {name: "Joske", code: "IEDDK"},
  {name: "Mieke", code: "IEDDK"},
  {name: "Jan", code: "IEDDK"},
  {name: "Test", code: "IsxJK"},
  {name: "Koen", code: "IsxJK"},
]

And I need a function which gives me an array of objects where there is only once occurrence of the code property:

[
  {name: "Joske", code: "IEDDK"},
  {name: "Test", code: "IsxJK"},
]

I have thought about it and I think I'm making it harder than it needs to be. Does someone has a simple function to do this?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

If you don't mind a "last wins", this can be done nicely with

[...new Map(data.map(d => [d.code, d])).values()]

with O(n) complexity (unlike some other approaches that re-iterate the array for every member, looking for matches thereby exhibiting quadratic complexity).

For a "first wins", just reverse your data:

[...new Map([...data].reverse().map(d => [d.code, d])).values()]

This can be folded into a typesafe generic function:

const distinctBy = <T, K>(data: T[], keySelector: (v: T) => K): T[] => 
    [...new Map(data.map(d => [keySelector(d), d])).values()]

and used

distinctBy(data, d => d.code)

Playground Link

about 4 years ago · Juan Pablo Isaza Report

0

With filter and findIndex is also an option:

const arr = [
  {name: "Joske", code: "IEDDK"},
  {name: "Mieke", code: "IEDDK"},
  {name: "Jan", code: "IEDDK"},
  {name: "Test", code: "IsxJK"},
  {name: "Koen", code: "IsxJK"},
];

const isTheSameAs = ({ code }) => item => item.code === code;

const filteredArr = arr
    .filter((item, indexInOriginalArray, array) => array.findIndex(isTheSameAs(item)) === indexInOriginalArray);

console.log(filteredArr);

about 4 years ago · Juan Pablo Isaza Report

0

Group your array based on code using array#reduce in an object accumulator and extract all the values using Object.values().

const data = [{ name: "Joske", code: "IEDDK" }, { name: "Mieke", code: "IEDDK" }, { name: "Jan", code: "IEDDK" }, { name: "Test", code: "IsxJK" }, { name: "Koen", code: "IsxJK" }, ],
    result = Object.values(data.reduce((r, o) => {
      r[o.code] = r[o.code] || o;
      return r;
    },{}));
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!