为空时替换一个值(ZAPIER - Javascript)
Posted
技术标签:
【中文标题】为空时替换一个值(ZAPIER - Javascript)【英文标题】:Replace a value when it is empty (ZAPIER - Javascript) 【发布时间】:2021-12-06 13:20:34 【问题描述】:我想从一个 URL 中提取 3 个 UTM(utm_source、utm_medium、utm_campaign),并使用 zapier 中的这个 javascript 代码步骤将它们发送到下一步。
javascript 代码运行良好:
let url = inputData.URL;
let queryString = url.split("?").pop();
let urlParams = new URLSearchParams(queryString);
let utm_source = urlParams.get('utm_source')
let utm_campaign = urlParams.get('utm_campaign')
let utm_medium = urlParams.get('utm_medium')
let utm_term = urlParams.get('utm_term')
let utm_content = urlParams.get('utm_content')
output = [url, queryString, utm_source, utm_campaign, utm_medium, utm_term, utm_content];
当 URL 没有 UTM 时,问题就出现了。 在这种情况下,该值为空,因此我想将其替换为默认值(例如“有机”)
我开始尝试这样的东西,但它不起作用:
if(utm_campaign === null || typeof utm_campaign === "undefined" || utm_campaign === "")
utm_campaign = "ORGANIC";
拜托,¿有人可以帮我处理这段 javascript 代码吗? 提前非常感谢您
【问题讨论】:
【参考方案1】:这是使用ternary
operator的绝佳机会:
let url = inputData.URL;
let queryString = url.split("?").pop();
let urlParams = new URLSearchParams(queryString);
let utm_source = urlParams.get('utm_source') ? urlParams.get('utm_source') : 'definitely not organic'
let utm_campaign = urlParams.get('utm_campaign')
let utm_medium = urlParams.get('utm_medium') ? urlParams.get('utm_medium') : 'organic'
let utm_term = urlParams.get('utm_term')
let utm_content = urlParams.get('utm_content')
output = [url, queryString, utm_source, utm_campaign, utm_medium, utm_term, utm_content];
在这种情况下,您可以为每个变量选择一个默认值。
【讨论】:
以上是关于为空时替换一个值(ZAPIER - Javascript)的主要内容,如果未能解决你的问题,请参考以下文章
当结构中的所有值都为空时,如何在 Scala spark 中使结构为空?