Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

119
Views
Replace double for loop but getting array instead of object

Beginner here...

So I wanted to make what I thought was a simple change, but it has proven to be more difficult. I have a .js script that runs on a timer as a function. The function queries data and brings back a list of objects that turn into an array of ids of records in the first loop.

In its original format -

    // get pledge objects
    const pledges = await getOppPledges()
    
    //Check if there are records
    if (pledges && pledges.length > 0) {
        
        //get array of pledge opportunity ids
        for (let pledge of pledges) {
            let oppIds = pledges.map(p => p.Opportunity__c)
            let sfOpps = await getOpp(oppIds)
            
            //run loop to create oppty doc
            for await (let opp of sfOpps) {...
            }
         ...}

My problem is that the double "for" loop generates double documents when there is more than one pledge. When I tried closing the first loop right before the second loop, "sfOpps" was unavailable for the second loop, and the code broke.

I had rearranged the code to something like,

   if (pledges && pledges.length > 0) {
        for (let pledge of pledges) {
            let oppIds = [pledge.Opportunity__c]
            let opp = await getOpp(oppIds)
            

But it seems like it returns "opp" as an array instead of an object. This does not work either because it comes back as undefined when I call for a value of "opp" like "opp.OpportunityContactRoles.totalSize".

Any input is appreciated!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If I understood your problem correctly you shouldn't be needing the first loop as you map your ids on all your pledges inside the loop. It's the reason why you do the operations n times. (n being the length of the "pledges" array)

let oppIds = pledges.map(p => p.Opportunity__c) // You're already mapping all the ids at once

The correct way to do it would be

// get pledge objects
const pledges = await getOppPledges()

//Check if there are records
if (pledges && pledges.length > 0) {
    
    //get array of pledge opportunity ids
    let oppIds = pledges.map(p => p.Opportunity__c)
    let sfOpps = await getOpp(oppIds)
        
    //run loop to create oppty doc
    for await (let opp of sfOpps) {...
    }
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!