javascriptnode.jsarraysapihubspot-crm

How to extract the month values from the res.body.results array of objects using JS Array map()


Also trying to get that custom code that extracts the months from the string with regex in my code snippet. I believe I am close but not quite. Console log is returning "undefined" values for the key/value pairs and 0 for the months when it should return 60. Any thoughts on how to restructure this would be very much appreciated! I am trying to get the highest number of months/years from an array and set it to a property in HubSpot. Thank you kindly for any advice on how to properly configure to get correct values.

          hubspotClient.crm.lineItems.batchApi.read({
            inputs,
            properties: ['hs_recurring_billing_period', 'recurringbillingfrequency',]
            })
            .then(res => {
              const inputs = res.body.results.map(result => {
                result.properties.recurringbillingfrequency = 
                result.properties.recurringbillingfrequency;
                result.properties.months = Number(result.properties.months);
                return { term: hs_recurring_billing_period, frequency: recurringbillingfrequency };
                                   
              })
              console.log(inputs);
              
              let term = 0;
              const largestNum = (years) => {
                
              //let term = 0;  
              for (let i=0; i <res.body.results.length; i++){
              let { recurringbillingfrequency, hs_recurring_billing_period } = 
              res.body.results[i].properties;
              console.log(recurringbillingfrequency, hs_recurring_billing_period)
              
              if(recurringbillingfrequency = "Annually")
              {
                let months = Number(hs_recurring_billing_period.replace(/\D/g, ''));
                let years = months / 12;

                
                // let term = 0;
                if (years[i] > term) {
                  term = years[i];
                }
              }
            }

                return term;
              }
              console.log(largestNum(term));
              return;

Solution

  • The map function looks strange to me:

    const inputs = res.body.results.map(result => {
      result.properties.recurringbillingfrequency = result.properties.recurringbillingfrequency;
      result.properties.months = Number(result.properties.months);
      return { term: hs_recurring_billing_period, frequency: recurringbillingfrequency };                            
    })
    

    within the scope of the mapping function, recurringbillingfrequency and hs_recurring_billing_period in the return object are not defined. Would it work by replacing the return value with as so?

    return { 
      hs_recurring_billing_period: result.properties.hs_recurring_billing_period, 
      recurringbillingfrequency: result.properties.recurringbillingfrequency
    };                            
    

    Also, I am not quite sure how this line is necessary:

    result.properties.recurringbillingfrequency = result.properties.recurringbillingfrequency;