Background of my issue:
To start I had an array of objects each containing a user-ID called entries. I had to make an api call using the id inside each of those object to get the users full profile containing their fullName. I was able to map over the results of the api call to get a list of all users that had matching id's to the initial list I started with and with that I also pulled out their fullName this variable is labeled matches. So now I have the initial list [array] of userID's entries, along with a list of arrays of all the ID matches I found inside that list from my api call matches this list of arrays contains the ID and a Name.
What I am trying to do now is map over the entries and matches to compare userID's and if a match is found then I want to insert the name inside matches into the entry the match is found in based on the index. However I am not sure how to properly insert a key value pair inside of and object based on index or if im approaching this the correct way. Here is my code:
useEffect(() => {
const loadAllProviders = async () => {
//TODO: Make conditional if entries exists
try {
//get all providers
const results = await api.Providers.listnoAudit({ scope: "all" });
const allProviders = results.items;
//map over the entries to get providerId's out
const providerIDs = entries.map((entry) => entry.createdBy);
//map over response to get out provider names and id's
const idName = allProviders.map((provider) => [provider.id, provider.fullName]);
//find if any returned providers have matching ids to the ones in entries
const matches = idName.filter((match) => providerIDs.includes(match[0]));
//TODO: map over entries
// if matches[0] = entries.createdby then
//push matches[1] (name) into the object at the index where match occurs
entries.map((entry, idx) => {
matches.map((provider) => {
if (entry.createdBy === provider[0]) {
let en = {...entry, fullName: provider[1]}
}
})
});
} catch (err) {}
};
loadAllProviders();
}, [entries]);
Inside my if statement youll see my attempt to add the fullName to the entry using the spread operator but Im not sure how to actually replace the old object with the new object. When I console out en, I get the items that have been modified... I am pretty new to coding and any advice would be helpful.