1

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.

1 Answer 1

1

You can probably reduce your allProviders list to get what you want. See the following example.

let allProviders = [
    {id: 1, name: "Amy"}, 
    {id: 2, name: "Kamala"}, 
    {id: 3, name: "Stacy"}, 
    {id: 4, name: "Sunil"}
]

let entries = [{createdBy: 2}, {createdBy: 4}]

let providerIds = entries.map(e => e.createdBy)

let result = allProviders.reduce((matches, provider) => {
  if(providerIds.includes(provider.id)) {
    matches.push({ ...provider, createdBy: provider.id})
  }
  return matches;
},[])

console.dir(result)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.