I am creating a shallow copy of an array that is obtained through activatedRoute to use the IDs found in each object of that array. That shallow copy is being created trought filter(). Here is a code example:
const product = this.activatedRoute.snapshot.data.products.filter((element: Products) => {
if (this.activatedRoute.snapshot.parent.data.product.id !== null) {
return this.activatedRoute.snapshot.parent.data.product.id.indexOf(element._id) === 0;
} else {
return false;
}
});
The indexOf() method works perfectly when there is not even a match between ids, but the moment 2 ids make a slight match, it is going to always return the first of those X matches that occur. I have tried methods like find(), findIndex(), but I am not finding a way to return the exact id. How can I target the return of that filter() function?
I’ve tried several Array prototype methods, but none of them are satisfying the condition to return the exact result I need.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/findIndex
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf