简体   繁体   English

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

[英]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.控制台日志正在返回键/值对的“未定义”值,并在应该返回 60 的月份返回 0。任何关于如何重组它的想法都将非常感激。 I am trying to get the highest number of months/years from an array and set it to a property in HubSpot.我正在尝试从数组中获取最多的月数/年数,并将其设置为 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;

The map function looks strange to me: map function 对我来说看起来很奇怪:

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.在映射 function 的 scope 内,返回 object 中的recurringbillingfrequencyhs_recurring_billing_period未定义。 Would it work by replacing the return value with as so?用 as 替换返回值会起作用吗?

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;

So either this loop will work and extract the months and set to years or you can use Lodash with one line of code.因此,要么这个循环将工作并提取月份并设置为年,要么你可以使用 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM