I am trying to do some basic filtering of some of my API results, to not appear if the store already contains that data in its parent object (think of this scenario as you're updating the parent client side, but you've yet to post it to the server; so you don't need to see data that you might post up).
I am using the includes method, and it is returning false, though it makes no sense to me what so ever. For example:
Here is my data for existing Ids after adding the extra element:
0: {athleteId: "246f3ff3-a889-4027-9643-0f376eeba4ce"}
1: {athleteId: "d8564df2-1464-4547-b418-d1c4c75fe1fc"}
2: {athleteId: "e36db0a1-4fe9-482c-910c-fc8b87770401"}
3: {athleteId: "71630dfc-f03a-45c0-b72c-24190658fa76"}
As you can see, it is an array with 4 elements, okay so far so good.
Then for testing purposes I do
console.log("Exists: ", existingIds!.includes({athleteId: "71630dfc-f03a-45c0-b72c-24190658fa76"}))
This returns false.
However, if I replace the object notation there with say existingIds[3] then it will return true. The full code is as follows:
var apiData = await Agent.Coaches.availableAthletes(id);
if(this.coach !== null) {
var existingIds = this.coach.assignedAthletes?.map((x) => (
{
athleteId: x.athleteId
}
));
console.log("Existing Ids: ", existingIds);
if(existingIds?.length === 4) {
console.log("Test: ", existingIds[3])
console.log("Exists: ", existingIds!.includes({athleteId: "71630dfc-f03a-45c0-b72c-24190658fa76"}))
}
apiData = apiData.filter(x => !existingIds!.includes({athleteId: x.athleteId}))
//this.coach.assignedAthletes!.filter(x => apiData.includes(x));
}
What exactly am I doing wrong here? The idea would be simple to implement in C# (which is sorta what I am trying to do) where I am just getting the IDS so the map function would be the equivalent of like
assignedAthletes.Select(x => { x.athleteId });
then for the filter I would just do:
finalData = finalData.Where(x => !assignedAthletes.Contains(x.athleteId))
I've done this many times in C# and never faced the same odd behavior as I am in JS