如何解决如何使用 JS Array map() 从 res.body.results 对象数组中提取月份值
还尝试获取自定义代码,在我的代码片段中使用正则表达式从字符串中提取月份。我相信我很接近但不完全。控制台日志正在返回键/值对的“未定义”值,并返回 0 表示应返回 60 的月份。有关如何重组此内容的任何想法将不胜感激!我试图从数组中获取最多的月数/年数并将其设置为 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 };
})
在映射函数范围内,返回对象中的recurringbillingfrequency
和hs_recurring_billing_period
没有定义。用 as so 替换返回值会起作用吗?
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;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。