# API fetch and loop to table data

**URL:** <https://community.hubspot.com/t/api-fetch-and-loop-to-table-data/78900>\
**Category:** APIs & Integrations\
**Tags:** professional, timeline-and-extensions-apis, free\
**Created:** [April 7, 2023, 9:17am UTC](https://community.hubspot.com/t/api-fetch-and-loop-to-table-data/78900 "2023-04-07T09:17:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jun22](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jun22/32/34870_2.png) [@Jun22](https://community.hubspot.com/u/Jun22)\
**Post date:** [April 7, 2023, 9:17am UTC](https://community.hubspot.com/t/api-fetch-and-loop-to-table-data/78900/1 "2023-04-07T09:17:11Z")

</div>

Hi guys, just need some help few tweaks to my code the results is looping my items. I should have 6 items to display only but the results on the image below it got loop redundant here’s my code. repeated the all list of items twice or trice

![Jun22_0-1680858948632.png](https://d2qpr2jzcazej7.cloudfront.net/original/2X/d/dab216bc578cc586f40d907ea0afbb14da6dc726.png)

```lia-code-sample
async function loadDealsDetails2() {
 try {
 const dealsResponse = await fetch(
 'https://transforminator.com/_hcms/api/congratsdeal',
 requestOptions
 )
 const dealsData = await dealsResponse.json()
 console.log(dealsData)
 if (dealsData.length > 0) { 
 // Create a list of unique contact IDs
 const contactsIds = dealsData.reduce((acc, curr) => {
 const partnerContact = curr.associations.contacts.results.find(
 (result) => result.type === 'partner_contact' || curr.associations.contacts.results[0]
 )
 return [...new Set([...acc, partnerContact.id])]
 }, [])

 // Create a list of all unique company IDs
 const companiesIds = dealsData.reduce((acc, curr) => {
 const partnerCompany = curr.associations.companies.results.find(
 (result) => result.type === 'partner_company' || curr.associations.companies.results[0]
 )
 return [...new Set([...acc, partnerCompany.id])]
 }, [])

 // Create a list of deal owners
 const dealOwnersIds = dealsData.map(({ properties }) => properties.hubspot_owner_id)

 // Fetch contacts, companies, and owners details 
 const contactsDetails = await fetchObjectsDetails('contactdetails', contactsIds.join(','))
 const companyiesDetails = await fetchObjectsDetails('companydetails', companiesIds.join(',')) 
 const ownersDetails = await fetchObjectsDetails('ownerdetails', dealOwnersIds.join(','))

 const userCompanyId = document.getElementById('companyId').value 

 if (dealsData.length > 0) {
 const table = document
 .getElementById('demoC')
 .getElementsByTagName('tbody')[0]
 const companyIdd = document.getElementById('companyId').value

 dealsData.forEach((element) => {
 const row = table.insertRow()
 let cell = row.insertCell()
 cell.innerHTML =
 '<a class="hyper_linkhere_trigger_javascript" target="_blank" href="/deal-details?dealid=' +
 element['id'] +
 '">' +
 element['properties']['dealname'] +
 '</a>'
 cell = row.insertCell()
 cell.innerHTML =
 getCompanyName(companyiesDetails, element['associations']['companies']['results'][0]['id'])
 cell = row.insertCell()
 cell.innerHTML = getOwnerDetails(ownersDetails, element['properties']['hubspot_owner_id'])
 cell = row.insertCell()
 cell.innerHTML = getContactDetails(contactsDetails, element['associations']['contacts']['results'][0]['id']).fullName 
 cell = row.insertCell()
 cell.innerHTML = getLabelName(element['properties']['dealtype'])
 cell = row.insertCell()
 cell.innerHTML = '$' + element['properties']['amount']
 cell = row.insertCell()
 cell.innerHTML = getLabelName(element['properties']['dealstage'])
 })
 } 
 }
 } catch (error) {
 console.log('error', error)
 }
 }

 loadDealsDetails()
 loadDealsDetails2()

```

 ![ScreenShot Tool -20230407170220.png](https://d2qpr2jzcazej7.cloudfront.net/original/3X/d/3/d31e31f17f412261bb53f465b1d56aedb78c413b.png)

---

<div class="post-metadata">

**Author:** ![PamCotton](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/pamcotton/32/47916_2.png) [@PamCotton](https://community.hubspot.com/u/PamCotton)\
**Post date:** [April 10, 2023, 3:52pm UTC](https://community.hubspot.com/t/api-fetch-and-loop-to-table-data/78900/2 "2023-04-10T15:52:00Z")

</div>

Hello @Jun22 ,

Thank you for posting in our Community! I want to invite our top experts to this conversation @stefen @Teun @Kevin-C any recommendations to @Jun22?

Thank you,

Pam

---

<div class="post-metadata">

**Author:** ![Kevin-C](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/kevin-c/32/34570_2.png) [@Kevin-C](https://community.hubspot.com/u/Kevin-C)\
**Post date:** [April 14, 2023, 2:09pm UTC](https://community.hubspot.com/t/api-fetch-and-loop-to-table-data/78900/3 "2023-04-14T14:09:45Z")

</div>

Thanks @PamCotton  
Hey @Jun22  
So without more context I think we’re a little limitted in the help we can provided. There may be 2 things worth looking into:

1. You may be calling redundant functions: “loadDealsDetails() loadDealsDetails2()”
2. ​You may have duplicates in you response data. Table log out your data before rendering it to ensure you’re getting the expected data.

Hope this gets you moving!
