1

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?

4 Answers 4

1

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

Sign up to request clarification or add additional context in comments.

Comments

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);

Comments

0

You coud use a forEach with find method, this way:

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

const removeSimilar = (array) => {
  let result = [];
  array.forEach((item) => {
    if (!result.find((i) => i.code === item.code)) result.push(item);
  });
  return result;
};

console.log(removeSimilar(data))

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.