-2

i want to eliminate duplicate object from an a list of arrays

i reshape the first array 'issueComment' using map and than use the filter to remove duplicate object but it doesnt work

const shapeCodes = this.issueComment.map(p => [p.ShapeCode, (this.lang === 'fr') ? 
  p.ShapeDescriptionFrench : p.ShapeDescriptionEnglish,
  p.AdminVCCQuestionnaireInstanceID]).filter((value, index, self) => self.indexOf(value) === index);

console output:

6) [Array(3), Array(3), Array(3), Array(3), Array(3), Array(3)]
0: (3) ["Transit_right_14", "Glace porte passager", 3504406]
1: (3) ["Transit_right_14", "Glace porte passager", 3504406]
2: (3) ["Transit_right_14", "Glace porte passager", 3504406]
3: (3) ["Transit_right_14", "Glace porte passager", 3504406]
4: (3) ["Transit_right_14", "Glace porte passager", 3504406] 
5: (3) ["Transit_right_14", "Glace porte passager", 3504406]
rezion
  • 7
  • 1
  • 4
  • this question has been answered many many times, and it's not a Typescript question, rather a javascript one. – Rachid O Jun 12 '20 at 02:08
  • i have seen examples for duplication for an array of objects yes but not a list of arrays – rezion Jun 12 '20 at 02:29
  • arrays and objects are compared by reference, not value. Two array literals will always create two different arrays, so `[[]].indexOf([])` will always be -1 – Jonas Wilms Jun 12 '20 at 14:28
  • Does this answer your question? [How to compare arrays in JavaScript?](https://stackoverflow.com/questions/7837456/how-to-compare-arrays-in-javascript) – Jonas Wilms Jun 12 '20 at 14:29

1 Answers1

0

The issue with objects is that they are compared by reference, and since each array is a unique reference, they will not be considered equivalent when using ===. you will have to compare each element one by one for equivalency.

// below will log out 'false'
const a:number[] = [1];
const b:number[] = [1];
console.log(a === b);

// below will log out `true`
const a:number[] = [1];
const b = a;
console.log(a === b);
vykaash
  • 56
  • 4