Get the index of duplicate values except the first
- With
.map()
, add the index of duplicated values into the array. For non-duplicate value, will be added withundefined
value [Will be filtered in Step 2]. - With
.filter()
, remove the value which isundefined
from the array.
const attD = [1, 2, 3, 4, 3, 3];
const findDuplicates = attD => attD.map((item, index) => {
if (attD.indexOf(item) !== index)
return index;
}).filter(x => x); // Equivalent to .filter(x => x != undefined || x != null)
const duplicates = findDuplicates(attD);
console.log(duplicates);