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?
Advertisement
Answer
const testArr = ['1,A', '2,B', '1,D', '3,A']; const result = testArr.reduce((val, cur) => { if (!val.some((v) => v.includes(cur[0]))) { val.push(cur); } return val; }, []); console.log(result);