Just tack an array.filter
on the end to remove all duplicates.
.filter((item, index, array) =>
index === array.findIndex((t) => (
t.name === item.name && t.id === item.id
))
)
which basically says
For each item in the array, perform a test to see if we need to filter
it out. The test is: find the first matching object with the same name
and id as the one we're testing. If the index of that found object is
the same as the index of the one we're testing, let it through. If
it's a different index (as in, we found a duplicate later on) discard
it.
Here is a testable snippet:
this.genesList = ['fred','john','dave','john','dave']
this.data = {probes:[{name:'john', id:2}]}
this.comparedGenesList = this.genesList.map((item) => {
const foundedItem = this.data.probes.find(({name}) => name.toUpperCase() === item.toUpperCase());
if (foundedItem !== undefined) {
return {
name: foundedItem.name.trim(),
matched: true,
id: foundedItem.id
};
} else {
return {
name: item.trim(),
matched: false,
id: undefined
};
}
}).filter((item, index, array) =>
index === array.findIndex((t) => (
t.name === item.name && t.id === item.id
))
)
console.log(this.comparedGenesList )