如何使用 JS Array map() 从 res.body.results 对象数组中提取月份值

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

还尝试获取自定义代码,在我的代码片段中使用正则表达式从字符串中提取月份。我相信我很接近但不完全是。控制台日志是 returning key/value 对的“未定义”值和 0 应该 return 60 的月份。任何关于如何重组它的想法将不胜感激!我正在尝试从数组中获取最大数量的 months/years 并将其设置为 HubSpot 中的 属性。感谢您提供有关如何正确配置以获得正确值的任何建议。

          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;

地图功能对我来说很奇怪:

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 };                            
})

映射函数范围内,return对象中的recurringbillingfrequencyhs_recurring_billing_period未定义。将 return 值替换为这样是否有效?

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

此外,我不太确定这条线是如何必要的:

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

因此,要么这个循环起作用并提取月份并设置为年份,要么您可以通过一行代码使用 Lodash。

              let term = 0;  
              for (let i=0; i <inputs.length; i++){
              let { recurringbillingfrequency, hs_recurring_billing_period } = 
              inputs[i];
              console.log(recurringbillingfrequency, hs_recurring_billing_period)
              
              if(recurringbillingfrequency.toLowerCase() === 'annually')
              {
                let months = hs_recurring_billing_period;

                let years = months / 12.0;
                /*
                 let highest = 0;
                 function getHighestTerm(values) {
                   for (let j=0; j < values.length; j++)
                  
                   if (j === 0) {
                     highest = values;
                   } else if (highest > values[j]) {
                     highest = values[j];
                   }
                   return highest;
                 }
                
                */
                term = _.max(_.map(inputs, 'hs_recurring_billing_period')) / 12.0;