Skip to content

Javascript remove the occurrences of item by matching condition

I have an arrray as below:

var testArr = [ "1,A", "2,B",  "1,D", "3,A" ]

I would like to get the result as

var result = [ "1,A", "2,B", "3,A" ]

I have tried to using filter but I cannot get the output. Could anyone please help me for this?


const testArr = ['1,A', '2,B', '1,D', '3,A'];
const result = testArr.reduce((val, cur) => {
  if (!val.some((v) => v.includes(cur[0]))) {
  return val;
}, []);